← Back to docs

Authentication

Protect keys and use the correct headers.

Keep API keys in environment variables. Never put keys in source code, URLs, screenshots or logs. Send them only to trusted servers and rotate exposed keys.

Install the libraries used by the example, use a supported runtime and set API_KEY in the environment. Replace sample model names with an available model matching the protocol.

Authentication

Authenticate with either x-api-key or Authorization: Bearer:

Send only one credential. If both headers are present, Authorization: Bearer wins; the gateway falls back to x-api-key only when Authorization does not start with "Bearer ". A wrong key in the winning header still returns 401 even if the other header is correct.

x-api-key: sk-thapi-...
Authorization: Bearer sk-thapi-...

The new key is saved as a hash code for authentication and AES-256-GCM encryption so that the owner can view or copy it back in the control panel. Old keys created before this feature cannot be restored. Each account can have a maximum of 10 active keys at the same time.

Create a separate key per machine or app: if a key leaks, you only revoke that one, and the Usage page can split cost by key. Do not paste keys into source code pushed to Git — put them in environment variables or a config file outside the repository.

Snippets are protocol-verified; run a small test call and verify the Usage log before deploying to production.