# Configure HTTP as a Snowbridge target
> Configure HTTP target for Snowplow Snowbridge to send data over HTTP with authentication, OAuth2, request templating, and response rules.
> Source: https://docs.snowplow.io/docs/api-reference/snowbridge/configuration/targets/http/
> Documentation index: https://docs.snowplow.io/llms.txt

[Snowplow supported](/docs/destinations/#destinations-catalog "Built and supported by Snowplow") · Snowbridge target · Streaming

> **Note:** Version 4.0.0 makes breaking changes to the HTTP target. Details on migrating can be found [in the migration guide](/docs/api-reference/snowbridge/upgrade-guides/upgrade-guide-4-X-X/)

> **Note:** Version 5.0.0 makes breaking changes to the HTTP target. Details on migrating can be found [in the migration guide](/docs/api-reference/snowbridge/upgrade-guides/upgrade-guide-5-X-X/)

> **Note: Breaking changes in version 6.0.0**
>
> Version 6.0.0 makes breaking changes to the HTTP target's OAuth2 configuration. Details on migrating can be found [in the migration guide](/docs/api-reference/snowbridge/upgrade-guides/upgrade-guide-6-X-X/)

## Basic authentication

Where basicauth is used, it may be configured using the `basic_auth_username` and `basic_auth_password` options. Where an authorization header is used, it may be set via the `headers` option.

We recommend using environment variables for sensitive values - which can be done via HCL's native `env.MY_ENV_VAR` format (as seen below).

TLS may be configured by providing the `key_file`, `cert_file` and `ca_file` options with paths to the relevant TLS files.

## OAuth2

Snowbridge supports sending authorized requests to OAuth2-compliant HTTP targets, via one of two flows. The `oauth_client {}` and `oauth_jwt {}` blocks are mutually exclusive — configuring both fails at startup.

Like in the case of basic authentication, we recommend using environment variables for sensitive values.

> **Note: Configuration changed in version 6.0.0**
>
> Before version 6.0.0, the client credentials flow was configured through top-level `oauth2_*` settings. See the [6.x upgrade guide](/docs/api-reference/snowbridge/upgrade-guides/upgrade-guide-6-X-X/) for how to migrate.

### Client credentials flow

Configure the `oauth_client {}` block with `client_id`, `client_secret`, and `refresh_token` (these three are long-lived credentials used to generate short-lived bearer access tokens), and `token_url` (which is the URL of the authorization server providing access tokens).

### JWT bearer flow

Applies to **Snowbridge 6.0.0+**[Latest: 6.1.0](/docs/api-reference/versions/#feature-availability-by-version "The latest Snowbridge version is 6.1.0")

Where the target expects a [JWT bearer assertion](https://datatracker.ietf.org/doc/html/rfc7523) instead of client credentials — as service accounts on Google APIs do, for example — configure the `oauth_jwt {}` block. Snowbridge signs a JWT with your private key and exchanges it for an access token at `token_url`.

The private key must be PEM-encoded; RSA (PKCS#1 and PKCS#8) and EC keys are supported. Supply it either inline via `key`, or as a path to a key file via `key_file`. These two are mutually exclusive.

| Setting     | Required                   | Description                                                                    |
| ----------- | -------------------------- | ------------------------------------------------------------------------------ |
| `key`       | One of `key` or `key_file` | PEM-encoded private key, supplied as a string.                                 |
| `key_file`  | One of `key` or `key_file` | Path to a file containing the PEM-encoded private key.                         |
| `issuer`    | Yes                        | Issuer claim (`iss`) — typically a service account email or client identifier. |
| `token_url` | Yes                        | URL of the authorization server providing access tokens.                       |
| `subject`   | No                         | Subject claim (`sub`), for impersonation flows.                                |
| `audience`  | No                         | Audience claim (`aud`). Defaults to `token_url`.                               |
| `scopes`    | No                         | Scopes to request in the token exchange.                                       |

## Dynamic headers

> **Note:** This feature was added in version 2.3.0

When enabled, this feature attaches a header to the data according to what your transformation provides in the `HTTPHeaders` field of `engineProtocol`. Data is batched independently per each dynamic header value before requests are sent.

## Request templating

> **Note:** This feature was added in version 3.0.0

This feature allows you to provide a [Golang text template](https://pkg.go.dev/text/template) to construct a request body from a batch of data. This feature should be useful in constructing requests to send to an API, for example.

Input data must be valid JSON, any message that fails to be marshaled to JSON will be treated as invalid and sent to the failure target. Equally, if an attempt to template a batch of data results in an error, then all messages in the batch will be considered invalid and sent to the failure target.

Where the dynamic headers feature is enabled, data is split into batches according to the provided header value, and the templater will operate on each batch separately.

### Helper functions

In addition to all base functions available in the Go text/template package, the following custom functions are available for convenience:

`prettyPrint` - Because the input to the templater is a Go data structure, simply providing a reference to an object field won't output it as JSON. `prettyPrint` converts the data to prettified JSON. Use it wherever you expect a JSON object in the output. This is compatible with any data type, but it shouldn't be necessary if the data is not an object.

`env` - Allows you to set and refer to an env var in your template. Use it when your request body must contain sensitive data, for example an API key.

### Template example

The following example provides an API key via environment variable, and iterates the batch to provide JSON-formatted data one by one into a new key, inserting a comma before all but the first event.

```hcl
loading...
```

[View on GitHub](<&#xA;https://github.com/snowplow/snowbridge/blob/v6.1.0/assets/docs/configuration/targets/http-template-full-example.file&#xA;>)

## Response rules (beta)

> **Note:** This feature was added in version 3.0.0
>
> This feature is in beta status because we may make breaking changes in future versions.
>
> **Breaking change in version 4.0.0**: Response rules are now evaluated in the order they are defined in the configuration. Rules must specify a `type` attribute to distinguish between invalid, setup, and throttle errors, rather than organizing them in separate `invalid` and `setup` blocks.

Response rules allow you to configure how the app deals with failures in sending the data. You can configure a response code and an optional string match on the response body to determine how a failure response is handled. Response codes between 200 and 299 are considered successful, and are not handled by this feature.

**Response rules are evaluated in the order they are defined in your configuration.** The first matching rule determines how the error is categorized.

There are four categories of failure:

`invalid` means that the data is considered incompatible with the target for some reason. For example, you may have defined a mapping for a given API, but the event happens to have null data for a required field. In this instance, retrying the data won't fix the issue, so you would configure an invalid response rule, identifying which responses indicate this scenario.

Data that matches an invalid response rule is sent to the failure target.

`setup` means that this error is not retryable, but is something which can only be resolved by a change in configuration or a change to the target. An example of this is an authentication failure - retrying won't fix the issue; the resolution is to grant the appropriate permissions, or provide the correct API key.

Data that matches a setup response rule is handled by a retry as determined in the `setup` configuration block of [retry configuration](/docs/api-reference/snowbridge/configuration/retries/).

`throttle` (added in version 4.0.0) is a special type of error that indicates the target is rate limiting requests. This is handled separately from transient errors to allow different retry behavior - typically with longer delays to respect rate limits.

Data that matches a throttle response rule is handled by a retry as determined in the `throttle` configuration block of [retry configuration](/docs/api-reference/snowbridge/configuration/retries/).

`transient` errors are everything else - we assume that the issue is temporary and retrying will resolve the problem. There is no explicit configuration for transient - rather, anything that is not configured as one of the other types is considered transient.

## Configuration options

Here is an example of the minimum required configuration:

```hcl
loading...
```

[View on GitHub](<&#xA;https://github.com/snowplow/snowbridge/blob/v6.1.0/assets/docs/configuration/targets/http-minimal-example.hcl&#xA;>)

If you want to use this as a [failure target](/docs/api-reference/snowbridge/concepts/failure-model/#failure-targets), then use failure\_target instead of target. Here is an example of every configuration option:

```hcl
loading...
```

[View on GitHub](<&#xA;https://github.com/snowplow/snowbridge/blob/v6.1.0/assets/docs/configuration/targets/http-full-example.hcl&#xA;>)

If you want to use this as a [failure target](/docs/api-reference/snowbridge/concepts/failure-model/#failure-targets), then use failure\_target instead of target.

## Example for Google Tag Manager Server Side

You can use the HTTP target to send events to Google Tag Manager Server Side, where the [Snowplow Client tag](/docs/destinations/forwarding-events/google-tag-manager-server-side/snowplow-client-for-gtm-ss/) is installed.

To do this, you will need to include a [transformation](/docs/api-reference/snowbridge/concepts/transformations/) that converts your events to JSON — [`spEnrichedToJson`](/docs/api-reference/snowbridge/configuration/transformations/builtin/spEnrichedToJson/).

Here’s an example configuration. Replace `<your-gtm-host>` with the hostname of your Google Tag Manager instance, and — optionally — `<preview-token>` with your preview mode token.

```hcl
target {
  use "http" {
    url                        = "https://<your-gtm-host>/com.snowplowanalytics.snowplow/enriched"
    request_timeout_in_seconds = 5
    content_type               = "application/json"

    # this line is optional, in case you want to send events to GTM Preview Mode
    headers                    = "{\"x-gtm-server-preview\": \"<preview-token>\"}"
  }
}

transform {
  use "spEnrichedToJson" {}
}
```
