Introduction
When building applications on top of Tinfoil’s private inference API, you often need to add your own infrastructure layer between your client applications and the inference enclave. Perhaps you want to authenticate your users, track API usage, implement rate limiting, or add custom routing logic. The challenge is doing this without compromising the end-to-end encryption. This is where the Encrypted HTTP Body Protocol (EHBP) becomes useful. EHBP encrypts HTTP message bodies at the application layer using Hybrid Public Key Encryption (HPKE), completely separate from transport layer security like TLS. This separation means you can run a proxy server that inspects headers and metadata, while the actual request and response bodies remain encrypted end-to-end between your client and the Tinfoil enclave. Moreover, our SDKs automatically check and validate the HPKE key to ensure it is generated inside an attested secure enclave. Your proxy server becomes an intermediary that can be used add your Tinfoil API keys, authenticate and authorize your users with custom logic, track usage metrics and implement rate limiting, add custom routing headers based on your business logic, and log metadata for debugging and monitoring—all while the actual inference data remains encrypted throughout the entire journey.The Challenge
When you’re building a production application with Tinfoil, you typically face a dilemma. On one hand, you need to keep yourTINFOIL_API_KEY secret—exposing it to client browsers would allow anyone to use your API quota.
On the other hand, you want your sensitive prompts and completions to be encrypted end-to-end to the attested enclave, not just to your backend server.
The Solution using EHBP
Your proxy server sits between your client and the Tinfoil enclave, handling authentication and metadata, while the actual inference data flows through it encrypted. The proxy receives encrypted requests from your client application, adds yourTINFOIL_API_KEY as the Authorization header, preserves the encryption headers required by the protocol, forwards the encrypted payload to Tinfoil’s enclave, and returns the encrypted response to your client.
This architecture gives you control over what the proxy can and cannot access.
The proxy has full visibility into HTTP headers, allowing you to read custom request headers (like X-User-ID or X-Request-ID) for authentication or routing decisions, add custom response headers (like X-Rate-Limit-Remaining or X-Request-Cost) for rate limiting or cost tracking, and log metadata for debugging and monitoring.
But the request and response bodies—the actual prompts, completions, and inference data—remain encrypted from client to enclave using keys the proxy never possesses.
How It Works
The data flow shows how encryption is maintained throughout the request lifecycle:Client Setup
Setting up the client is straightforward. First, install the Tinfoil SDK:Proxy support with
SecureClient is currently only available in the Node.js SDK.baseURL points to your proxy server where requests are sent, while the enclaveURL points to the Tinfoil enclave whose public key is used for encryption.
This configuration ensures that even though your proxy handles the HTTP routing, only the enclave can decrypt the request bodies.
await client.ready(), the client fetches the enclave’s public key from the enclaveURL and performs verification.
From that point on, all request bodies are encrypted using that key before being sent to your proxy, and all response bodies are decrypted after being received.
Proxy Server Setup
One of the important aspects of EHBP is that the proxy requires no special cryptographic libraries or dependencies. Since the proxy doesn’t participate in encryption or decryption, it only needs basic HTTP handling capabilities available in any web framework. Your proxy’s job is simple: preserve the EHBP protocol headers that coordinate encryption between client and enclave, add your authentication credentials, and forward the encrypted payload. While we provide example implementations in Go and Node.js below, the proxy pattern works with any language that can handle HTTP requests. The example repository provides a complete reference implementation that you can adapt to Python, Rust, or any other language.Required Headers to Preserve
The EHBP protocol uses specific headers to coordinate encryption keys between the client and enclave. Your proxy must forward these headers unchanged in both directions. For requests, you need to preserveEhbp-Client-Public-Key (the client’s public key for encrypting responses) and Ehbp-Encapsulated-Key (the encrypted symmetric key for the request body).
For responses, you need to preserve Ehbp-Encapsulated-Key (the encrypted symmetric key for the response body), Ehbp-Client-Public-Key (echoed back from the request), and Ehbp-Fallback (indicates if the server responded in plaintext when fallback is enabled).
Understanding the body framing format isn’t necessary for proxy implementation, but it helps explain why these headers are critical—they contain the ephemeral keys needed to decrypt each chunk of the encrypted stream.
CORS Configuration
If your proxy will be called from browser-based applications, you’ll need to configure CORS headers to allow the browser to send and read the encryption headers. The key requirement is exposing the EHBP headers in both directions:Implementation Examples
Working with Custom Headers
Beyond the required EHBP headers, you can use custom headers to implement your own application-level protocols between the client and proxy. This is where you build your authentication, rate limiting, user tracking, and other business logic.Request Headers
Your client can include custom headers that the proxy reads but the enclave never sees. This is perfect for user authentication tokens, request IDs, feature flags, or any other metadata:Access-Control-Allow-Headers configuration so browsers can send them.
Response Headers
Similarly, your proxy can add custom headers to responses that the client can read. This is useful for communicating rate limit information, cost tracking, request IDs for debugging, or any other metadata:Access-Control-Expose-Headers configuration for browsers to access them.
Security Considerations
While EHBP encrypts the request and response bodies, the HTTP headers remain visible throughout the proxy chain. This design is intentional—it’s what allows proxies to route and manage requests—but it has important security implications.Always keep your
TINFOIL_API_KEY secret. Never expose it to client applications. The entire purpose of the proxy architecture is to keep this key on your backend.Ehbp-Client-Public-Key, Ehbp-Encapsulated-Key, and Ehbp-Fallback). These headers are critical for the encryption protocol to work. Dropping or modifying them will break the end-to-end encryption between client and enclave.
Next Steps
For a complete working example, check out the encrypted-request-proxy-example repository. It includes a Go proxy implementation, TypeScript client with streaming support, custom header handling for authentication and rate limiting, and comprehensive error handling.Example Repository
Complete proxy server example with Go and TypeScript
Encrypted HTTP Body Protocol
Deep dive into the EHBP specification

