SDKs
JavaScript/Typescript
Advanced Concepts
Interceptors

Interceptors

The TradrAPI Typescript SDK makes use of Axios under the hood in order to perform HTTP requests. This means that you can use the same interceptor functionality that Axios provides, within the SDK itself to intercept requests and responses and apply any logic or mutations that you may need.

Further information on Axios interceptors can be found here (opens in a new tab).

import { AxiosResponse } from 'axios';
import { Tradr } from '@tradrapi/trading-sdk';
 
// Define a function that will run on every outgoing request
function simpleLoggingInterceptor(response: AxiosResponse) {
  console.log(`Logging ${response.status} response from ${response.config.url}`);
  return response;
}
 
const tradrApi = Tradr.make({
  auth: { apiKey: 'MY_API_KEY' },
  http: { interceptors: { response: [simpleLoggingInterceptor] } },
});
 
tradrApi.accounts.get(12) // Make requests as normal

The function, simpleLoggingInterceptor will then run on every incoming response.

Multiple interceptors may be defined and assigned to either the request or the response, these will then be run in the order that they are defined.

Interceptors may also be defined to the request property, which will run on before outgoing request.