We have created a demo company and got the Client id and client secret id. We have generated the Auth code using postman tool. We are trying to further hit the other Invoice related APIs like 'create contact', 'invoice' etc.
We have tried calling other APIs (create invoice, contact etc) from Xero API Explorer. We have successfully done that, In API Explorer there are by default authorization code and Tenant id.
But we are not able to get the tenant id from our Demo company of Xero portal.
Please help us to get the tenant id ??
Loading


Jayraj ChhayaRank #304 · 8.2K reputationPosted Jan 3, 2024, 12:47 AM EST
To retrieve the tenant ID for your demo company in the Xero portal, you can follow these steps:
Make a GET request to the
/connectionsendpoint of the Xero API using the authorization code you obtained from Postman. This endpoint will provide you with a list of connections associated with your account.Look for the connection that corresponds to your demo company. Each connection will have a unique
tenantIdproperty.Retrieve the
tenantIdvalue from the connection object. This is your tenant ID.Here's an example of how you can achieve this using the Xero API SDK in Python:
import requests import json authorization_code = "YOUR_AUTHORIZATION_CODE" client_id = "YOUR_CLIENT_ID" client_secret = "YOUR_CLIENT_SECRET" # Exchange authorization code for access token token_url = "https://identity.xero.com/connect/token" payload = { "grant_type": "authorization_code", "code": authorization_code, "client_id": client_id, "client_secret": client_secret, "redirect_uri": "YOUR_REDIRECT_URI" } response = requests.post(token_url, data=payload) access_token = response.json()["access_token"] # Make a GET request to retrieve connections connections_url = "https://api.xero.com/connections" headers = { "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } response = requests.get(connections_url, headers=headers) connections = response.json() # Find the connection for your demo company demo_company = None for connection in connections: if connection["tenantType"] == "ORGANISATION": demo_company = connection break # Retrieve the tenant ID tenant_id = demo_company["tenantId"] print(f"Tenant ID: {tenant_id}")By following these steps and making the appropriate API calls, you should be able to retrieve the tenant ID for your demo company in the Xero portal.
Sachin SinghRank #66 · 26.2K reputationPosted Jan 3, 2024, 1:24 AM EST
You can use the below code
var accessToken = "your_access_token"; var apiUrl = "https://api.xero.com/connections"; using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var response = await httpClient.GetAsync(apiUrl); if (response.IsSuccessStatusCode) { var jsonResponse = await response.Content.ReadAsStringAsync(); // Parse the JSON response to get the tenant ID // The structure of the JSON response may vary based on Xero API version // Extract the tenant ID based on your specific API version and response structure } else { // Handle error var errorMessage = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Error: {errorMessage}"); } }