Last updated

API client methods

The API clients each expose the following configuration and utility methods you can use to customize your instance.

addRequestInterceptor

addRequestInterceptor({thenDelegate, catchDelegateopt})

Adds a request interceptor to the current API instance. Wrapped around Axios' request interceptor.

Example

api.addRequestInterceptor({
  thenDelegate: (config) => {
    config.params['extra-query-param'] = 'foobar';
    return config;
  },
});

Parameters

Name Type Attribute Description

thenDelegate

Function

Defines the delegate logic to execute when the request is completed. Receives the request configuration as a parameter. Must return the configuration for the request chain to continue.
:::js thenDelegate(config) => {Object}

Parameters

Name TypeDescription
configObjectThe request configuration. Must be returned after being modified.

catchDelegate

Function

Optional

Defines a callback to execute before the catch block of the request is executed for this interceptor.
:::js catchDelegate(error) => Promise

Parameters

Name TypeDescription
errorObjectThe request error. Should be resolved as a Promise. This method can be used to prevent certain errors from being caught.

removeRequestInterceptor

removeRequestInterceptor(interceptor)

Removes a specific request interceptor from the current API instance.

Example

// set reference to interceptor
const interceptor = api.addRequestInterceptor({
  thenDelegate: (config) => {
    config.params['extra-query-param'] = 'foobar';
    return config;
  },
});
// remove via reference
api.removeRequestInterceptor(interceptor);

Parameters

NameTypeAttributeDescription
interceptorFunction-The reference to the previously added request interceptor that should be removed from the current instance.

addResponseInterceptor

addResponseInterceptor({thenDelegate, catchDelegateopt})

Adds a response interceptor to the current API instance. Wrapped around Axios' response interceptor.

Example

api.addResponseInterceptor({
  thenDelegate: (response) => {
    // modify reponse data before having it processed by the API client
    response.data.shift(); //removed first element
    return response;
  },
});

Parameters

Name Type Attribute Description

thenDelegate

Function

Defines the delegate logic to execute when the request is completed. Receives the API response as a parameter. Must return the response chain to continue.
:::js thenDelegate(config) => {Object}

Parameters

Name TypeDescription
responseObjectThe API response. Must be returned after being modified.

catchDelegate

Function

Optional

Defines a callback to execute before the catch block of the response is executed for this interceptor.
:::js catchDelegate(error) => Promise

Parameters

Name TypeDescription
errorObjectThe response error. Should be resolved as a Promise. This method can be used to prevent certain errors from being caught.

removeResponseInterceptor

removeResponseInterceptor(interceptor)

Removes a specific response interceptor from the current API instance.

Example

// set reference to interceptor
const interceptor = api.addResponseInterceptor({
  thenDelegate: (response) => {
    response.data.shift();
    return config;
  },
});
// remove via reference
api.removeResponseInterceptor(interceptor);

Parameters

NameTypeAttributeDescription
interceptorFunction-The reference to the previously added response interceptor that should be removed from the current instance.

setTimeout

setTimeout(timeout)

Define the default timeout delay in milliseconds for the current API instance.

Example

api.setTimeout(10000);

Parameters

NameTypeAttributeDescription
timeoutnumber-Timeout delay in milliseconds.

setSessionToken

setSessionToken(token)

Use a JWT session token to identify the API requests. This removes the private API key header if present. This method of authentication should be applied instead of the private API key when the client is used in a browser.

To retrieve a session token, first initialize the API client without an API key and use the sign in resource to login the user to Rebilly. The token will be available in the response fields.

For example usage, see the Usage with JWT sections in each API client above.

setEndpoints

setEndpoints({liveopt, sandboxopt})

Update the endpoints URL for live, sandbox or both mode in the current API instance's active URL. This is useful for testing a different version of the API.

Securing communications

When modifying the API endpoints always use HTTPS for a production environment.

Example

api.setEndpoints({ live: 'https://api.rebilly.com/experimental/version/url' });

Parameters

NameTypeAttributeDescription
livestringoptionalURL for the live API mode.
sandboxstringoptionalURL for the sandbox API mode.

setProxyAgent

setProxyAgent({host, port, auth})

Define a proxy for the current API instance. Authorized using HTTP Basic credentials.

Example

const config = {
  host: '127.0.0.1',
  port: 9000,
  auth: {
    // HTTP Basic
    username: 'foobar',
    password: 'fuubar',
  },
};
// all subsequent API requests will pass through the proxy
api.setProxyAgent(config);

Parameters

NameTypeAttributeDescription
hostStringHostname of the proxy server.
portNumberPort of the proxy server.

auth

Object

Basic credentials to connect to the proxy server.

Properties

Name TypeDescription
usernameStringThe username required for basic authentication.
passwordStringThe password required for basic authentication.