JWT authentication
Sign short-lived tokens on your server for Web SDK calls and API requests
Sign tokens only on your server. Never send your private API key to a browser, a mobile app, or any other client.
A Vapi JSON Web Token (JWT) is a short-lived credential that you sign on your server with your private API key. Vapi doesn’t issue these tokens. Your server creates them, and Vapi checks the signature on each request.
Use a public JWT in the Vapi Web SDK in place of a public API key. Each token expires within 1 hour and can carry its own origin and assistant restrictions.
How it works
Your server signs a JWT with the HS256 algorithm, using your private API key as the secret. Your client sends the token in the Authorization header, or passes it to the Vapi Web SDK. Vapi verifies the signature against each private API key in the organization named by the token’s orgId claim.
The token.tag claim sets what the token can be used for:
A public JWT works only with the Web SDK, and a private JWT can’t start Web SDK calls. If you leave out the token claim, the JWT is treated as public.
Prerequisites
- A private API key and your organization ID
- A server that runs Node.js 18 or later, where you can store the private API key as a secret
- An assistant to use in Web SDK calls
Signing requirements
Every Vapi JWT must meet these requirements:
Don’t set the iss claim to https://api.vapi.ai. That issuer is reserved for tokens Vapi creates, and your token fails verification. Other top-level claims are ignored.
Token claim fields
The token claim accepts these fields:
Create a public token
Store your credentials on the server
Set your private API key and organization ID as environment variables on your server:
Sign the token
Save this script as create-public-token.mjs, replace the example origin and assistant ID with your own, then run node create-public-token.mjs:
In a real app, create the token in a server endpoint that your client calls, and return the token in the response.
Start a web call with the token
If you haven’t installed the Web SDK in your client project, run npm install @vapi-ai/web.
Pass the token to the Vapi Web SDK in place of a public API key:
Before each new call, request a fresh token from your server and create a new Vapi instance with it. The SDK keeps the token it received when constructed, so an existing instance will continue using an expired token.
Restrict a public token
Restrictions limit where a public token works and which assistants it can use. Set them in token.restrictions. Restrictions apply only to public tokens, and a request that breaks one fails with a 403 response.
Allowed origins follow these rules:
- Enter each origin as a scheme, host, and optional port. Any path is ignored.
- Match the scheme, host, and port to the
Originheader the browser sends. - Use
*in place of one whole subdomain label. For example,https://*.example.commatcheshttps://app.example.combut nothttps://example.comorhttps://a.b.example.com. - Don’t use partial wildcards, such as
https://myapp-*.example.com. They never match.
Create a private token
A private JWT works with the same endpoints as a private API key. Use it to give another service short-lived API access without sharing your private API key.
Sign it the same way as a public token, with tag set to "private". Save this script as create-private-token.mjs, then run node create-private-token.mjs:
Send the token in the Authorization header as a bearer token, the same way you send a private API key.
Verify it works
To check a private token, sign one and use it to list your assistants. Save this script as verify-private-token.mjs, then run node verify-private-token.mjs:
A 200 status and a JSON list of your assistants confirm that your server signs tokens correctly.
If your organization has no assistants, a 200 response with an empty list ([]) still confirms that Vapi accepted the token.
To check a public token, start a web call from one of its allowed origins. The call starts if the token is valid. If the request fails with a 401 or 403 response, see Troubleshoot token errors.
Troubleshoot token errors
A 401 response means the token failed verification. A 403 response means a public token’s restrictions rejected the request.