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

Data Days is here! Join us now for 60+ days of learning, challenges, and connection. Learn more

Reply
Anonymous
Not applicable

Creating datasource using Power BI RestAPI

I tried creating "Excel" datasource using PowerBI Rest API. When am calling the CreateDatasource rest api call with parameters am getting error as "Bad request".

Below is the code:

private void btn_createdatasource(object sender, RoutedEventArgs e)
{
PowerBIClient pbiClient = TokenManager.GetPowerBiClient(requiredScopes);
var gateways = pbiClient.Gateways.GetGateways().Value;
var gateway = gateways.First();
var gatewaydatasources = pbiClient.Gateways.GetDatasources(gateway.Id).Value;

// string userName= ConfigurationManager.AppSettings.Get("username");
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;


var datatype = "Excel";
var name = "excelsource";
var password = ConfigurationManager.AppSettings.Get("Password");
var credentials = new WindowsCredentials(userName, password);
var credentialsEncryptor = new AsymmetricKeyEncryptor(gateway.PublicKey);
var credentialDetails = new CredentialDetails(credentials, PrivacyLevel.Private, EncryptedConnection.Encrypted, credentialsEncryptor);


var path =
@"C:\Users\sowmyas\Documents\ReportserverExcelfiles\SuperstoreSales.xlsx";
try {
var request = new PublishDatasourceToGatewayRequest(
dataSourceType: datatype,
connectionDetails: "{ \"path\":\"" + path + "\"}",
credentialDetails: credentialDetails,
dataSourceName: name);

pbiClient.Gateways.CreateDatasource(gateway.Id, request);
MessageBox.Show("Gateway Datasource successfully : " + name);
}
catch (Exception ex)
{
MessageBox.Show("Datasource not created in Gateway. Error: " + ex.Message);
}
}

 

Error Message:

 

sowmyas_1-1635855443402.png

  Name Value Type

$exception{"Operation returned an invalid status code 'BadRequest'"}Microsoft.Rest.HttpOperationException

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous,

It seems like you send the wrong request content to the rest API so that the power bi server return the bad request error.

If you are not so sure how to configuration Datasource connections settings, you can try to publish a sample report to the power bi service side with a similar type of data source and use rest API to get the detailed configuration. After these steps, you can refer to this template to config your own data source settings.

Gateways - Get Datasource - REST API (Power BI Power BI REST APIs) | Microsoft Docs

Regards,

Xiaoxin Sheng

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @Anonymous,

It seems like you send the wrong request content to the rest API so that the power bi server return the bad request error.

If you are not so sure how to configuration Datasource connections settings, you can try to publish a sample report to the power bi service side with a similar type of data source and use rest API to get the detailed configuration. After these steps, you can refer to this template to config your own data source settings.

Gateways - Get Datasource - REST API (Power BI Power BI REST APIs) | Microsoft Docs

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

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.

Top Kudoed Authors