> ## Documentation Index
> Fetch the complete documentation index at: https://docs.phala.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Encrypt a request

> Send field-level encrypted requests using Phala’s attested gateway key.

Use your Phala API key with `https://inference.phala.com/v1`. Every model on this endpoint requires verified upstream serving.

E2EE encrypts selected request and response fields between your client and the attested gateway.
TLS still protects the connection; E2EE adds field-level encryption whose public key comes from the
gateway's [attested keyset](https://docs.phala.com/phala-cloud/confidential-ai/verify/verify-attestation.md). This guide shows how to send an
E2EE request. For the trust model and what E2EE does and does not protect, read
[End-to-end encryption](https://docs.phala.com/phala-cloud/confidential-ai/confidential-model/e2ee-encryption.md).

> [!NOTE]
>
> E2EE is decrypted inside the attested gateway so it can process your request. It proves the
> decryption key belongs to the attested workload; it does not hide the request from that workload. For
> the upstream to be attested too, use a [confidential model](https://docs.phala.com/phala-cloud/confidential-ai/confidential-model/api-reference/models.md).

## When to use it

Use E2EE when you want field-level encryption bound to a key you can trace to the attestation
report, in addition to TLS. If TLS is sufficient for your threat model, you can call the API normally
without these headers.

## Supported endpoints

E2EE v2 is supported only on `/v1/chat/completions` and `/v1/completions` for buffered or streaming
responses, and on `/v1/embeddings` for buffered responses. Every other inference endpoint rejects
authenticated E2EE requests with `400` and type `e2ee_unsupported_endpoint`. A missing bearer token
can be rejected with `401` before the E2EE check. See the
[gateway endpoint handlers](https://github.com/Dstack-TEE/private-ai-gateway/blob/main/src/http/app/handlers.rs).

## 1. Get the gateway E2EE key

Fetch the attestation report and read the E2EE public key from the attested keyset. Verify the report
first (see [Verify a response](https://docs.phala.com/phala-cloud/confidential-ai/verify/verify-signature.md)) so you know the key is bound to a genuine
TEE.

curl -s "[https://inference.phala.com/v1/aci/attestation?nonce=\$(openssl](https://inference.phala.com/v1/aci/attestation?nonce=\$\(openssl) rand -hex 32)" \\
-H "Authorization: Bearer \$PHALA\_AI\_API\_KEY" \\
\| jq -r '.attestation.workload\_keyset.e2ee\_public\_keys\[] | select(.key\_id=="dstack-kms-e2ee-x25519-v1") | .public\_key'

This selects `x25519-aes-256-gcm-hkdf-sha256`, recommended for browser-native clients. Existing
secp256k1 clients can instead select `dstack-kms-e2ee-v1`, whose algorithm is
`secp256k1-aes-256-gcm-hkdf-sha256`. Generate your client key on the same curve as the selected key.

## 2. Send E2EE headers

Include these headers on your request. Do not send `X-Signing-Algo`; that selects the legacy path.

| Header | Value |
| - | - |
| `X-E2EE-Version` | `2` |
| `X-Client-Pub-Key` | Your public key for the selected suite, hex encoded. |
| `X-Model-Pub-Key` | The gateway E2EE public key from step 1. |
| `X-E2EE-Nonce` | 32 fresh random bytes encoded as 64 hexadecimal characters, without `0x`. |
| `X-E2EE-Timestamp` | Current Unix seconds, within 300 seconds of gateway time by default. |

## 3. Encrypt the selected fields

Each encrypted field is the lowercase hex encoding of:

```text theme={"system"}
ephemeral_public_key || aes_gcm_nonce (12 bytes) || ciphertext || tag (16 bytes)
```

The ephemeral public key is 32 raw bytes for X25519 or 65 uncompressed SEC1 bytes for secp256k1.
Use a fresh ephemeral key and AES-GCM nonce per field.

The AEAD associated data binds the ciphertext to the protocol version, direction, algorithm, model,
field path, nonce, and timestamp. Any mismatch fails decryption.

> [!WARNING]
>
> Assembling the ciphertext and AAD by hand is error-prone. Use the official client or verifier
> helpers, which implement the key exchange, AAD construction, and field selection for you.

## Receipts with E2EE

An encrypted response carries `X-E2EE-Applied: true`, `X-E2EE-Version: 2`, and `X-E2EE-Algo` set to
the selected key's algorithm, alongside `x-receipt-id`. These headers are hints, not cryptographic
proof.

Reproduce `request.received.body_hash` by replacing encrypted request fields with their decrypted,
restored values and compactly serializing the JSON body. Hash this serialization, not the encrypted
request bytes.

For `response.returned.body_hash`, hash the exact JSON or SSE wire bytes, including ciphertext and
streaming framing, before decrypting response fields. Separately verify each field's AEAD tag and
associated data. See [Receipts with E2EE](https://docs.phala.com/phala-cloud/confidential-ai/confidential-model/e2ee-encryption.md#receipts-with-e2ee).

## Related

> [card] [E2EE concept](https://docs.phala.com/phala-cloud/confidential-ai/confidential-model/e2ee-encryption.md)

> [card] [Verify a response](https://docs.phala.com/phala-cloud/confidential-ai/verify/verify-signature.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.