# Relay

_Evervault Relay is a configurable proxy that encrypts or decrypts sensitive data in transit for your APIs and third‑party services._

![An illustration of Relay decrypting data in transit]()

Relay is a network proxy that encrypts and decrypts sensitive data in transit between a client and a destination server. It works with your own APIs and third-party services, and requires minimal changes to your existing code.

## How Relay works

You can use Relay in a few ways.

- **Outbound from your server**: You hold encrypted data (such as [card details](/cards/card-collection)) and forward it to a third-party API. Relay decrypts the data as the request passes through it, so your infrastructure never handles sensitive data directly.
- **Inbound to your server**: Data arrives from an external source and needs to be encrypted before it reaches your server. Relay encrypts the fields you specify on the way in.
- **Outbound to your frontend**: Encrypted card details are decrypted on the way out, so they can be displayed to customers in a browser through the [Reveal component](/cards/reveal).

> To further process data encrypted by Relay, check out [Functions](/functions) and [Enclaves](/enclaves).

Relay is most commonly used for outbound decryption. Here's what that looks like for collecting and sharing card data for a payment.

![A flow diagram showing encrypted card data passing from a customer to your service, then through Relay, which decrypts it for the payment provider and returns the response]()

1. **Collect**: Your customer enters their card details into the [Card component](/cards/card-collection). The data is encrypted within the iframe before it's returned to your application.
2. **Store**: Your backend receives and stores the encrypted card information. Because the data is encrypted, it's safe to store on your side.
3. **Send**: Using the encrypted data, you send an API request through Relay to your payment processor.
4. **Decrypt**: Relay detects the encrypted card fields and decrypts them in transit.
5. **Process**: The payment processor receives the card data in plaintext and completes the payment. The response is piped back to you through Relay.

Evervault doesn't do anything with the response from your payment processor. If needed, you can configure Relay to encrypt any sensitive values before the response is forwarded to your server.

## Getting started

This example focuses on creating a Relay and how you can send encrypted data through it. See the [Card Collection](/cards/card-collection#get-started-with-the-card-component) page for an end-to-end guide that covers collecting, encrypting, and forwarding card data.

> Encrypt and decrypt actions are tied to app keys. If you encrypt data with Relay in a [sandbox app](/developers/sandbox), you can't decrypt that data with Relay from a production app (or the other way around). Data can only be encrypted and decrypted using the same app key.

### Create a Relay

Open the [Relays section](http://app.evervault.com/app/relays) in the dashboard and click **Create Relay**. Provide the base URL (`example.com`) for the API you want to proxy requests to and create the Relay.

Evervault provisions a domain for your Relay after creating it. This is the domain you'll send requests to when you want to proxy API calls to downstream partners. You can find the domain in the dashboard (it looks this: `[DESTINATION_URL].relay.evervault.app`).

### Configure a route

Routes define what to do with requests that match a given [URL path](#path-matching), and you can have multiple routes for each Relay. You configure the [data type and fields](#selecting-fields) (JSON, form data, etc.) to operate on, as well as what to do with that data. To create a route, click **+ Add Route** in the dashboard and then set the path (e.g., `/charge`), actions, etc.

You configure Relays to encrypt or decrypt data with route actions. These actions can be applied to requests (data being sent to the destination) or the response (data being returned to you). The combination you configure depends on your use case.

- Decrypt on request: your backend holds encrypted data and forwards it to a third-party API. Relay decrypts the fields in transit.
- Decrypt on response: used with the [Reveal component](/cards/reveal) to display card details to your customers.
- Encrypt on request: Encrypt data that you collect before storing it (outside of card data which you should use our [card collection](/cards/card-collection) for)
- Encrypt on response: encrypt data returned by a third party, such as a card issuer returning PANs, before it reaches your server.

### Sending a request

Pass your Relay's domain into your requests to proxy them to the target endpoint.

#### cURL

```bash
curl -X POST [YOUR_RELAY_URL] \
    -H "Content-Type: application/json" \
    -d '{ "message": "Hello, World!" }'

[ANNOTATION lines=1-1]
Replace the destination's domain with your Relay's domain. The path stays the same.
[/ANNOTATION]
```

#### Node

```javascript
fetch('[YOUR_RELAY_URL]', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ message: 'Hello, World!' }),
});

[ANNOTATION lines=1-1]
Replace the destination's domain with your Relay's domain. The path stays the same.
[/ANNOTATION]
```

#### Python

```python
import requests
import json

url = '[YOUR_RELAY_URL]'

data = {
  'message': 'Hello, World!'
}

requests.post(url, data=json.dumps(data), headers={'Content-Type': 'application/json'})

[ANNOTATION lines=4-4]
Replace the destination's domain with your Relay's domain. The path stays the same.
[/ANNOTATION]
```

#### Ruby

```ruby
require 'net/http'
require 'uri'
require 'json'

uri = URI('[YOUR_RELAY_URL]')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
request.body = { message: "Hello, World!" }.to_json
http.request(request)

[ANNOTATION lines=5-5]
Replace the destination's domain with your Relay's domain. The path stays the same.
[/ANNOTATION]
```

#### Go

```go
package main

import (
  "bytes"
  "fmt"
  "net/http"
)

func main() {
  url := "[YOUR_RELAY_URL]"
  data := []byte(`{"message":"Hello, World!"}`)

  resp, err := http.Post(url, "application/json", bytes.NewBuffer(data))
  if err != nil {
    panic(err)
  }
  defer resp.Body.Close()

  fmt.Println("Response status:", resp.Status)
}

[ANNOTATION lines=10-10]
Replace the destination's domain with your Relay's domain. The path stays the same.
[/ANNOTATION]
```

#### Java

```java
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;

public class Main {
    public static void main(String[] args) throws Exception {
        URL url = new URL("[YOUR_RELAY_URL]");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");

        String input = "{\"message\":\"Hello, World!\"}";

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        conn.disconnect();
    }
}

[ANNOTATION lines=7-7]
Replace the destination's domain with your Relay's domain. The path stays the same.
[/ANNOTATION]
```

#### PHP

```php
<?php
$url = '[YOUR_RELAY_URL]';
$data = array('message' => 'Hello, World!');

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_exec($curl);
curl_close($curl);
?>

[ANNOTATION lines=2-2]
Replace the destination's domain with your Relay's domain. The path stays the same.
[/ANNOTATION]
```

You can include a `traceparent` header on requests sent through Relay to correlate them with events in your own systems. Relay passes the header through to the destination and includes it in Evervault's logs, so you can look up a request later. Learn more about [correlation IDs](/developers/logs#correlation-ids).

As an example, when you collect a card number with Evervault, it's encrypted on the frontend before being sent to your backend. So a card number like `4242 4242 4242 4242` is turned into something like this.

```
ev:QkTC:LUZWpxaiu16W1DJT:AnzlRtDd/kWFWuOsQH+HE0oqZHKCYZst+yZAQAFVBiXh:xsbavmwYTzgtKFWEcA8o9eMT+9olCFYKkTOtyFX158IrXn+G8MAtALr0pZrxBh0:$
```

You store this value instead of the plaintext card number. When you want to charge the card, you [send the request through Relay](/cards/card-collection?backend=node#integrate-the-relay) with the encrypted string. Relay decrypts it in transit, and your downstream partner receives the plaintext card number.

## Path matching

Route actions will only be applied to requests that match the request path. Paths can be configured to match a specific path or multiple paths with a wildcard.

| Example | Description |
| --- | --- |
| `/` | Matches requests to the root path |
| `/checkout` | Matches requests to /checkout |
| `/patients/*/reports` | Allows for dynamic URL segments such as IDs. Matches requests such as /patients/1/reports, /patients/2/reports, etc. |
| `/**` | Matches all requests |

## Selecting fields

Route actions are only applied to data that matches the list of selected fields defined on an action. Field selection can be configured on a per-content type basis.

### JSON

Use [JSONPath](https://goessner.net/articles/JsonPath/) to select fields according to their path in the JSON object. JSON values must only contain encrypted strings (e.g., `{ "ssn": "ev:...1a2bc$" }`). If the value contains other data like raw text (e.g.,`{ "ssn": "User SSN: ev:...1a2bc$" }`), decryption fails.

JSON payloads are supported for requests where the `content-type` subtype is JSON. The subtype is the part of the content-type header after the `/`. For example, a content-type of `application/json+charset=utf8` has a subtype of JSON.

#### JSON selector examples

### Form data

Form data is only supported for requests with a `content-type` of `application/x-www-form-urlencoded` or `multipart/form-data`. Form fields are matched directly by name. If a matched value contains multiple pieces of encrypted text, each one is decrypted individually. Global wildcards are supported for form data but not nested ones (e.g., `*` is valid but something like `name.*.last` isn't).

> **Wildcard selectors also decrypt encrypted files**
>
> An `*` form-field decrypt selector also decrypts encrypted files that arrive as octet-streams, including file parts in `multipart/form-data`. This unlocks file-decryption flows and deviates from the field-level model used elsewhere. Only Evervault-encrypted files are decrypted, so plain, non-encrypted octet-streams pass through untouched.

#### Form data selector examples

### Files

Relay supports encrypting and decrypting files in `multipart` and `form-data` requests. Encrypted files are forwarded with an unchanged filename, but the bytes in the file are fully encrypted. There is a per-request limit of 25 MB for encryption and decryption.

An `*` [form data](#form-data) decrypt selector also matches encrypted files, so it isn't always necessary to configure a separate file selector.

#### File selector examples

### Headers

Headers can be selected by name and aren't case sensitive. If the matched header value contains multiple pieces of encrypted text, each one is decrypted individually.

#### Header selector examples

### XML

To work with XML data, configure your Relay for JSON and select the fields to encrypt or decrypt using standard JSONPath. To encrypt attributes, use advanced JSONPath selectors.

#### XML selector examples

## Authentication

API key authentication adds an additional layer of security to your Relay. When enabled, requests through Relay require a valid Evervault API key in the `X-Evervault-Api-Key` header and your app's ID in the `X-Evervault-App-Id` header.

> **Enable authentication for Relays that decrypt data**
>
> Evervault strongly recommends enabling API key authentication for any Relay configured to decrypt data. If your Relay is pointed at your own API (which already has its own authentication), it may not be strictly necessary. When using Relay with a third-party API, API key authentication ensures that only your application can trigger decryption.

You can enable authentication for your Relay in the [Evervault Dashboard](https://app.evervault.com) by navigating to the **Authentication settings** page.

> **Authorize access to decrypted data**
>
> Some flows deliver decrypted data directly to a client (e.g., the [Card Reveal](/cards/reveal) iframe fetching plaintext card data). In these cases, your backend endpoint is the only place where access control is enforced. Evervault has no context for which of your customers can access a given record, so authenticate the request and verify that the caller is authorized before the response is returned.

## Limitations

The following limitations apply to all requests sent through Relay. If your application requires higher limits, contact [support@evervault.com](mailto:support@evervault.com) to discuss your requirements.

| Description | Limit |
| --- | --- |
| Max payload size | 5mb |
| Max file size | 25mb |
| Max chunked size (transfer-encoding: chunked) | 700kb |
| Connection Timeout | 120s |

## Alerts

Alerts allow you to see errors in Slack or be sent to any webhook URL of your choice. You can create an alert for your Relay inside of the Evervault Dashboard by navigating to **Relay → Alerts → Add Alert**. When you configure a custom webhook to receive alerts, the following fields will be in the response.

**Payload**

- `relayUuid` — The unique identifier for your Relay.
- `teamUuid` — The unique identifier for your team.
- `errorMessage` — Message explaining what went wrong.
- `evMessage` — Message about where the error has occurred.
- `activityLink` — A link to view the failed transaction in the Evervault Dashboard.
- `timestamp` — The time the error occurred.
- `errorType` — The Relay [error code](#relay-error-codes).
- `statusCode` — The HTTP status code of the response.
- `statusGroup` — 4XX, 5XX, or ERR.
- `inboundDomain` — Your Relay domain.

## Error handling

Errors originating from within Relay can be distinguished by the `X-Evervault-Error-Code` header. The responses will have a content type of `application/problem+json` and a payload of the following structure:

```json
{
  "code": "error code used to pinpoint the specific issue in the reference below",
  "title": "human readable title for the error",
  "detail": "longer-form, human readable description of the error",
  "status": 0 // numeric http status code of the response
}
```

**Error Codes**

- `subdomain-not-found` — This error indicates you are sending a request to a Relay (e.g., my-api-com.relay.evervault.app) which doesn't exist. Ensure you have entered the Relay subdomain correctly. Also, ensure you have configured your Relay correctly by following our Relay documentation.
- `request-timeout` — The target took too long to respond. The error can have many causes ranging from network issues to malformed client requests. Ensure the target domain URL is correct, wait a few moments and try again. If the error persists, reach out to the owner of the target domain through any support channels they have in place.
- `service-unavailable` — The target refused the connection and couldn't be reached. Ensure the target host is healthy. We recommend that you implement retry behavior to mitigate the impact of brief issues with the network or target host.
- `unsupported-content-type-in-response` — This error occurs when Relay is configured to perform response encryption and receives an unsupported content type as a response from the endpoint. Currently, Relay can only encrypt JSON, XML, and multipart/form-data structured data.
- `proxy-auth-failed` — There was an issue authenticating with the provided API key. Ensure that you are using the correct API key.
- `content-length-required` — The Content-Length header is either not present or isn't a number.
- `invalid-request` — This error indicates that an invalid hostname was provided while communicating with Relay in [forward proxy mode](#forward-proxy-mode). Ensure you have entered the downstream hostname correctly, and that it can be resolved when requesting it directly.
- `forbidden` — The API key provided doesn't have sufficient grants to perform the actions requested. Please review the grants given to your API Key.
- `payload-too-large` — This request exceeds the request size limit in Relay. The current per-request limit is 25 MB for encryption/decryption of multipart data, and 5 MB for all other content types.
- `invalid-content-type` — The content type header received by Relay was malformed, preventing the body from being processed.
- `internal-server-error` — There was an issue with Relay itself. Try again in a few moments.
- `decrypt/forbidden` — A ciphertext's data policy prevented it from being decrypted. Ensure your data policy's configuration is correct.
- `no-hostname-in-request` — This error indicates that Relay received a CONNECT request with no hostname while operating in [forward proxy mode](#forward-proxy-mode). Ensure you have entered the downstream hostname correctly, and that it can be resolved when requesting it directly.
- `relay-not-found` — There was an issue finding a Relay for the provided domain. Make sure you are using the correct domain. If your domain is correct but you are still seeing this issue, ensure you have Relay configured correctly.
- `encrypt/pre-encrypted-data` — There was encrypted data where it wasn't expected. This can mean a number of things: - An inbound request header value contained Evervault encrypted data. - An inbound request payload had Evervault encrypted data as the value of one of the fields, which wasn't going to be encrypted. - The target URI for the inbound request had Evervault encrypted data as part of the path.
- `proxy-error` — Relay experienced an unexpected networking error while communicating with the downstream service. Please confirm that the downstream service is accessible when not routing through Relay, and ensure that any allowlist requirements have been met.
- `http-connect-not-allowed` — Relay received a request using the CONNECT method which is only supported in [forward proxy mode](#forward-proxy-mode).
- `bad-request` — The server is unable (or refuses) to process the request sent by the client, due to an issue that's perceived by the server to be a client problem.
- `proxy-authentication-required` — A Relay with authentication enabled was requested without valid credentials.

## Custom domains

Each Relay is provisioned with its own unique `relay.evervault.app` domain. You can also configure additional custom domains for your Relay in the Evervault Dashboard by navigating to **Relay → Settings → Domains**.

To add a custom domain, add a CNAME record to your DNS provider that points to `custom-dns.relay.evervault.app`. You also need to add an ownership validation TXT record on the `_ev-custom-domain` subdomain of your custom domain.

> DNS propagation can take anywhere from a few minutes to 24 hours depending on your DNS provider and the TTL settings on your domain. If your custom domain isn't resolving after adding the records, check that both the CNAME and TXT records have been added correctly and allow more time for propagation.

## Alternative configurations

These configurations are possible with Evervault, but not generally recommended. They're only used in specific situations where the default options aren't adequate.

### mTLS

mTLS (mutual TLS) lets Relay authenticate with the target server using a client certificate, adding an additional layer of transport security between Relay and the downstream service. This is typically only required by organizations with strict security policies that mandate mutual authentication at the network layer — most integrations don't need it.

> mTLS must be enabled by Evervault. Contact [support@evervault.com](mailto:support@evervault.com) to request it for your Relay.

### Forward proxy mode

The [integration described above](#getting-started) is sufficient for most use cases, but there are some scenarios where you might need to use Relay in forward proxy mode (e.g., you can't change the hostname used by a third party library). Forward proxy mode requires installing the Relay certificate chain, and configuring the relevant traffic to route using Evervault Relay acting as [an HTTP CONNECT proxy](https://en.wikipedia.org/wiki/HTTP_tunnel).

To integrate with Relay in forward-proxy mode, you need to:

- Trust the Relay cert chain on your server. The cert can be obtained from [ca.evervault.com](https://ca.evervault.com).
- Identify the best approach for establishing an HTTP tunnel in your preferred language.
  - For example, in Node.js this is likely using an Agent (which can be created using the [Evervault Node.js SDK](sdks/node#createrelayhttpsagent)), or in JVM-based languages this is through the VM's `http.proxyHost` configuration options.
- Supply your proxy authentication credentials to Relay using basic auth. This can be done during either the initial `CONNECT` request or the subsequent proxied request using the `Proxy-Authorization` header. The value should be basic auth credentials, with your app ID as the username and your API key as the password.
  - Relay only proxies requests to hosts that have been explicitly created in your app and can't be treated as a generic forward proxy.

You can test Relay as a forward proxy using curl:

```sh
curl -x https://relay.evervault.com -U app_id:api_key https://example.com/foo -kv
```

### Using fixed regions

> Using fixed regions isn't generally recommended and Evervault doesn't guarantee reliability or provide an SLA when using a fixed region.

Evervault operates an active-active infrastructure across multiple regions and availability zones. If you need to, you can specify specific regions (either `us` or `eu`) to use with Relay, but this doesn't come with an SLA from Evervault's side. To do this, add `us` or `eu` to your Relay URL when making a call. For example, if your base URL is `your-prod-app-12345.relay.evervault.app`, change it to `your-prod-app-12345.us.relay.evervault.app` to send requests to the US region.

- [Functions](/functions): Learn how to use Functions to process encrypted data with your own code.
- [Card Collection](/cards/card-collection): Learn how to use Card Collection to collect cardholder data.

