# API client methods The API clients each expose the following configuration and utility methods you can use to customize your instance. ## addRequestInterceptor div code strong addRequestInterceptor ({ span thenDelegate , span catchDelegate span opt }) Adds a request interceptor to the current API instance. Wrapped around Axios' request interceptor. **Example** ```js 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 | Type | Description | | --- | --- | --- | | `config` | Object | The 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 | Type | Description | | --- | --- | --- | | `error` | Object | The request error. Should be resolved as a Promise. This method can be used to prevent certain errors from being caught. | | ### removeRequestInterceptor div code strong removeRequestInterceptor ( span interceptor ) Removes a specific request interceptor from the current API instance. **Example** ```js // 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 div code strong addResponseInterceptor ({ span thenDelegate , span catchDelegate span opt }) Adds a response interceptor to the current API instance. Wrapped around Axios' response interceptor. **Example** ```js 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 | Type | Description | | --- | --- | --- | | `response` | Object | The 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 | Type | Description | | --- | --- | --- | | `error` | Object | The response error. Should be resolved as a Promise. This method can be used to prevent certain errors from being caught. | | ### removeResponseInterceptor div code strong removeResponseInterceptor ( span interceptor ) Removes a specific response interceptor from the current API instance. **Example** ```js // 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 div code strong setTimeout ( span timeout ) Define the default timeout delay in milliseconds for the current API instance. **Example** ```js api.setTimeout(10000); ``` **Parameters** | Name | Type | Attribute | Description | | --- | --- | --- | --- | | timeout | number | - | Timeout delay in milliseconds. | ## setSessionToken div code strong setSessionToken ( span 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 div code strong setEndpoints ({ span live span opt , span sandbox span opt }) 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** ```js 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 div code strong setProxyAgent ({ span host , span port , span auth }) Define a proxy for the current API instance. Authorized using **HTTP Basic** credentials. **Example** ```js 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** | Name | Type | Description | | --- | --- | --- | | `username` | String | The username required for basic authentication. | | `password` | String | The password required for basic authentication. | |