
How To Consume an API
To access an API, user/system needs to authentication using
Subscription Key
JWT Token
This section will list down all the steps to consume an API.
Login to the Sandvik API Catalogue Portal using AD Authentication
Browse to Products page
Explore APIs under a selected product
Explore the API definition and operations for each APIs
Create a subscription key for the selected product
The subscription key should be named as ISVCXXXX ConsumerName/SystemName (Contact integration team for ISVCXXXX. Later this information will be available on the developer portal)
Once the subscription key is approved by API Owner/Product Owner, consumer will be able to see the subscription key
Alternatively you can also contact Group IT integration team for the subscription key.
Order the App registration via Service Portal Link
Contact API owner/Group IT Integration Team to get the Application id white listed for the API consumption.
Steps to generate token
Once app registration is created and whitelisted. Kindly make a rest call to fetch the token from Azure AD.
Token URL: https://login.microsoftonline.com/e11cbe9c-f680-44b9-9d42-d705f740b888/oauth2/v2.0/token
Method: POST
Body:
{
"grant_type": "client_credentials",
"client_id": "<Client Id of App Registration>",
"client_secret": "<Client Secret of App Registration>",
"scope": "<Scope for the token>"
}
Scope Values:
Dev: 7ed07c00-623d-4b2c-9059-9b52ccc72fdd/.default
QA: 831aea61-6117-47b5-a7cb-c00d1a9e04f5/.default
Prod: c191f055-5f90-4442-a3c8-ad0c2e8479cc/.default
Once you generate the token add the request header
Authorization: Bearer <Generated Token>
Sample C# code to generate the token:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
var token = await GetTokenAsync("https://your-token-endpoint.com/token", "your-client-id", "your-client-secret", "your-scope");
Console.WriteLine($"Token: {token}");
}
static async Task<string> GetTokenAsync(string tokenEndpoint, string clientId, string clientSecret, string scope)
{
using (var client = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, tokenEndpoint);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret),
new KeyValuePair<string, string>("scope", scope)
});
request.Content = content;
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
// Assuming the response is in JSON format and contains an "access_token" field
var token = Newtonsoft.Json.Linq.JObject.Parse(responseContent)["access_token"].ToString();
return token;
}
else
{
throw new Exception($"Failed to retrieve token. Status code: {response.StatusCode}");
}
}
}
}