Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
nikolasNovak
Regular Visitor

Problem with creating datasource under gateway through API

Hi,
I am trying to create new datasources under gateway using api from C# but still gets error response "400: Bad request".

Supplied credentials encrypted as described in https://msdn.microsoft.com/en-us/library/mt784645.aspx

 

Here's code i'm using...

 

 string powerBIDatasourcesApiUrl = String.Format("https://api.powerbi.com/v1.0/myorg/gateways/{0}/datasources", gatewayId); 
HttpWebRequest request = System.Net.WebRequest.Create(powerBIDatasourcesApiUrl) as System.Net.HttpWebRequest;
//POST web request to create a datasource.
request.KeepAlive = true;
request.Method = "POST";
request.ContentLength = 0;
request.ContentType = "application/json";

//Add token to the request header
request.Headers.Add("Authorization", String.Format("Bearer {0}", token));

string datasourceJson = "{" + "\"name\":\"" + <datasourceName> + "\"," + "\"type\":\"Sql\"," + "\"connectionDetails\":\"{\"server\":\"<server>\",\"database\":\"<database>\"}\"," + "\"credentialDetails\":{" + "\"credentials\":\"" + <credentialsHash> + "\"," + "\"encryptionAlgorithm\":\"RSA-OAEP\"," + "\"encryptedConnection\":\"Encrypted\"," + "\"privacyLevel\":\"Public\"," + "\"credentialType\":\"Windows\"" + "}}";
//POST web request
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(datasourceJson);
request.ContentLength = byteArray.Length;

//Write JSON byte[] into a Stream
using (Stream writer = request.GetRequestStream())
{
writer.Write(byteArray, 0, byteArray.Length);
writer.Flush();
writer.Close();
writer.Dispose();

var response = (HttpWebResponse)request.GetResponse();

Console.WriteLine(string.Format("Datasource {0}", response.StatusCode.ToString()));
}

 

2 REPLIES 2
v-micsh-msft
Microsoft Employee
Microsoft Employee

You can't add a new datasource through the patch Datasource API, as this API is used to:

"

 updates the credentials for a data source.

"

As far as I know, currently there is no possible way to create new datasource, what could be done is:

Update the existing DataSource connection string, or update the credentials for a data source.

https://msdn.microsoft.com/en-US/library/mt771178.aspx

 

Regards,

Michael

Hi Michael,

thank for your reply. 

 

According to this manual,

https://msdn.microsoft.com/en-us/library/mt784643.aspx

it should be possible to create a new datasource under gateway.

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.