Configure your HTTP request

When making an HTTP request using any of the HTTP actions, you might need to authenticate the request, configure timeout settings, or add custom headers.

You can customize the request by using an Axios request configuration object as the last parameter of the action.

Request Configuration Limitations

The following config options are set by Cloudhooks, and shouldn't be overwritten:

  • url
  • method
  • data

Common Configuration Examples

Authentication

Here's how to call an API with bearer authentication:

module.exports = async function(payload, actions, context) {
  const apiToken = 'XXXXXXXXXXXXXXXXXXX';
  const httpConfig = {
    headers: {
      'Authorization': `Bearer ${apiToken}`
    }
  }

  const response = await actions.http.get(
  	'https://api.example.com/api-endpoint/',
    httpConfig
  )
}

Timeout Settings

To set a timeout for your request:

const httpConfig = {
  timeout: 5000, // timeout in milliseconds
}

Custom Headers

To add multiple custom headers:

const httpConfig = {
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'X-Custom-Header': 'custom-value'
  }
}

Proxy Configuration

If you need to route requests through a proxy:

const httpConfig = {
  proxy: {
    host: 'proxy.example.com',
    port: 9000,
    auth: {
      username: 'proxy_user',
      password: 'proxy_password'
    }
  }
}

For more configuration options, refer to the Axios request configuration documentation.

Note: All examples can be combined into a single configuration object as needed.