API quickstart
Introduction
This quickstart guide demonstrates how to authenticate using the Authentication API and retrieve wholesaler products using the Supply API. We'll use JavaScript for API calls.
Step 1: Obtain an Authentication Token
Endpoint: POST /authentication/api/v1/oauth2/token
// JavaScript Example:const body = {"clientId": "your clientId","clientSecret": "your clientSecret","audienceUrl": "your audienceUrl"};const headers = {'Content-Type': 'application/json'};fetch('https://api-test.cobuilder.no/authentication/api/v1/oauth2/token', {method: 'POST',body: JSON.stringify(body),headers: headers}).then(response => response.text()).then(data => console.log(data)).catch(err => console.error(err));
Step 2: Retrieve Wholesaler Products
Endpoint: GET /supply/api/v1/wholesaler-products
// JavaScript Example:const token = "your token from the previous call";const headers = {'Content-Type': 'application/json','Authorization': `Bearer ${token}`};fetch('https://api-test.cobuilder.no/supply/api/v1/wholesaler-products', {method: 'GET',headers: headers}).then(response => response.json()).then(data => console.log(data)).catch(err => console.error(err));
Combined Process
This demonstration combines authentication and product retrieval in a complete example.
// JavaScript Example:// Step 1: Get an authentication tokenconst authBody = {"clientId": "your clientId","clientSecret": "your clientSecret","audienceUrl": "your audienceUrl"};const authHeaders = {'Content-Type': 'application/json'};fetch('https://api-test.cobuilder.no/authentication/api/v1/oauth2/token', {method: 'POST',body: JSON.stringify(authBody),headers: authHeaders}).then(response => {if (!response.ok) {throw new Error('Authentication failed');}return response.text();}).then(token => {console.log('Token:', token);// Step 2: Use the token to make a Supply API callconst supplyHeaders = {'Content-Type': 'application/json','Authorization': `Bearer ${token}`};return fetch('https://api-test.cobuilder.no/supply/api/v1/wholesaler-products', {method: 'GET',headers: supplyHeaders});}).then(response => {if (!response.ok) {throw new Error('Supply API call failed');}return response.text();}).then(products => {console.log('Products:', products);}).catch(err => {console.error('Error:', err);});
Explore More
Now that you have successfully authenticated and retrieved wholesaler products, feel free to experiment with other endpoints in the API. Make sure to authenticate first by obtaining a valid token. Refer to the API documentation for a complete list of available endpoints and their details.