Making API Requests
You can use the code action to request information from external third-party applications using a package called request.
A common use case for code actions is to request information from external third-party applications through API calls.
In this guide, we provide you with examples of using Khoros Flow to make GET and POST requests. These requests are based on the popular axios package.
API Requests
An example of a GET request:
// GET Request
async payload => {
const result = await request.get("https://some-external-api")
console.info('Received data', result.data)
}
An example of a POST request:
// POST Request
async payload => {
const result = await request.post("https://some-external-api", {
orderId: '1234'
})
console.info('Received result', result.data)
}
Retrieve the Request API error codes
It is best practice to catch and handle various errors that may occur when making API calls.
An example request to retrieve the API error codes:
async payload => {
try {
// Make a request
const result = await request.get('MY API ENDPOINT')
// ...
} catch(err) {
if (err.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error('Failed with response', err.response.data)
} else if (err.request) {
// The request was made but no response was received
console.error('Failed request', err)
} else {
// Something happened in setting up the request that triggered an Error
console.error('Failed in general', err)
}
}
}
More information
See Axios reference for more information on API calls.
Updated 23 days ago