Skip to main content

View on GitHub

tinfoilsh/tinfoil-js

Overview

The Tinfoil JavaScript SDK is a wrapper around the OpenAI Node client that provides secure communication with Tinfoil enclaves. It has the same API as the OpenAI SDK with additional security features including automatic verification that the endpoint is running in a secure Tinfoil enclave, TLS certificate pinning, and attestation validation. All payloads are encrypted end-to-end using EHBP (Encrypted HTTP Body Protocol), which encrypts data directly to the attested enclave using HPKE (RFC 9180).

Installation

Requirements: Node 20+

Quick Start

Do not set baseURL unless you are routing through your own proxy server.

Migration from OpenAI

Migrating from OpenAI to Tinfoil is straightforward. The client is designed to be compatible with the OpenAI Node client:
All method signatures remain the same since TinfoilAI extends the standard OpenAI client.

Browser Support

The SDK supports browser environments for direct API access from web applications.
Security Warning: Using API keys directly in the browser exposes them to anyone who can view your page source. For production applications, always use a backend server to handle API keys.

Browser Usage

Browser Requirements:
  • Modern browsers with ES2020 support
  • Web Crypto API support for enclave verification

Running the Chat Example

To run the streaming chat example:
  1. Clone the repository
  2. Install dependencies:
  1. Create a .env file with your configuration:
  1. Run the example:
The example demonstrates streaming chat completions with the Tinfoil API wrapper.

Usage

Model Examples

Below are specific examples for each supported model. Click on any model to see its configuration and usage example.

Chat Models

Audio Models

Transcription

Audio Q&A

Embedding Models

API Documentation

This library is a drop-in replacement for the official OpenAI Node.js client that can be used with Tinfoil. All methods and types are identical. See the OpenAI client for complete API usage and documentation.

Advanced

Transport Modes

The SDK supports two transport modes via the transport option:
  • 'ehbp' (default): HPKE encryption via the Encrypted HTTP Body Protocol
  • 'tls': TLS certificate pinning (requires direct connection to the enclave)
EHBP is the preferred transport as it works in browsers and supports reverse proxies. TLS mode uses certificate pinning which only works in Node.js environments. See the EHBP documentation for details.

SecureClient

For custom integrations, the SDK exposes a SecureClient class with low-level access to the secure transport layer and verification information:

What the verifier checks

The JavaScript verifier runs the same code in Node.js and the browser. On each connection it enforces:
  • Hardware attestation: the AMD SEV-SNP report signature and the VCEK certificate chain back to the AMD root, with minimum firmware and TCB versions and debug mode disabled.
  • Code provenance: the Sigstore bundle for the expected release, verified against a bundled Sigstore trust root, including the Fulcio certificate chain, the signed certificate timestamp, Rekor transparency log inclusion, and a GitHub Actions identity for the expected repository on a tag ref.
  • Measurement match: the attested launch measurement equals the measurement in the signed release predicate.
  • Key binding: the TLS certificate’s subject alternative names commit to the attested HPKE public key and to a hash of the attestation document, and match the expected hostname.
Any failure throws an AttestationError and the client refuses to send data. The JavaScript verifier does not implement the freshness witness, revocation, or Intel TDX checks in the Go V3 verifier Beta. In particular:
  • verifiedAt in the verification document is the local clock reading when verification completed. It is not an attested time and not a freshness bound.
  • Release selection comes from the attestation bundle service, which serves the router’s current release. The verifier checks that release is validly signed and matches the enclave; it does not enforce a maximum release age or a minimum version. Compare releaseDigest in the verification document against the release you expect if you need rollback resistance. See In-band vs. out-of-band verification.
  • The attestation bundle service only supports AMD SEV-SNP routers. A request for a TDX enclave returns 501 Not Implemented, which surfaces as a FetchError.
For connection-time freshness and revocation checks, use Go SDK, Swift SDK, or CLI releases with beta V3 verification, or run the CLI as an audit-time check alongside a JavaScript client.