Last updated 5 months ago

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 run 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
NameTypeDescription
configObjectThe request configuration. Must be returned after being modified.
catchDelegate Function Optional Defines a callback to run before the catch block of the request is executed for this interceptor.

:::js catchDelegate(error) => Promise

Parameters
NameTypeDescription
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

Name Type Attribute Description
interceptor Function - 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 run when the response is completed. Receives the API response as a parameter. Must return the response chain to continue.

:::js thenDelegate(response) => {Object}

Parameters
NameTypeDescription
responseObjectThe API response. Must be returned after being modified.
catchDelegate Function Optional Defines a callback to run before the catch block of the response is executed for this interceptor.

:::js catchDelegate(error) => Promise

Parameters
NameTypeDescription
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

Name Type Attribute Description
interceptor Function - 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

Name Type Attribute Description
timeout number - 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

Name Type Attribute Description
live string optional URL for the live API mode.
sandbox string optional URL 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

Name Type Attribute Description
host string - Hostname of the proxy server.
port number - Port of the proxy server.
auth Object - Basic credentials to connect to the proxy server.

Properties
NameTypeDescription
usernamestringThe username required for basic authentication.
passwordstringThe password required for basic authentication.