Consume API

In this example, we will try to fetch data from any api using axios.

It is a straightforward process where we first install the Axios dependency and then use it to call the API and retrieve the response data.

In our case, the API is secured using OAuth 2.0 Client Credentials authentication. Therefore, we first make an Axios request to obtain an access token. Once the token is generated, we use it in a second Axios request to authenticate and consume the API.

Install axios

terminal
npm install axios@latest

Sample Code logic

custom-logic.js
// Replace <region> with your actual region in the baseUrl
const baseUrl = 'https://auditlog-management.cfapps.<region>.hana.ondemand.com';


const clientId = 'sb-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxb6316';
const clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

const tokenURL = 'https://<subdomain>.authentication.<region>.hana.ondemand.com/oauth/token';

const endpoint = '/auditlog/v2/auditlogrecords?time_from=2026-07-10T00:00:00&time_to=2026-07-10T00:03:00';

// Axios POST call to Generate Token
const tokenResponse = await axios.post(tokenURL, qs.stringify(
    {
        grant_type: 'client_credentials'
    }
), {
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic ' + Buffer.from(`${clientId}:${clientSecret}`).toString('base64')
    }
});


const accessToken = await tokenResponse.data.access_token;

const apiUri = baseUrl + endpoint;

// Axios GET call to fetch API data
const apiResponse = await axios.get(apiUri, {
    headers: {
        'Authorization': `Bearer ${accessToken}`,
        'Accept': 'application/json'
    }
});

const data = apiResponse.data ;

!!! Its Done !!!