I've asked a few weeks ago about JWT
# general-english
e
I've asked a few weeks ago about JWT creation for the webclient, it still seems to be broken. Aside from that the docs seem to have changed and they are now linking to an non existing page. https://docs.vapi.ai/customization/jwt-authentication The code:
Copy code
ts
        const payload = {
          orgId: process.env.VAPI_ORG_ID,
          // This is the scope of the token
          token: {
            tag: 'public',
            restrictions: {
              enabled: process.env.NODE_ENV !== 'development',
              allowedOrigins: [process.env.NEXT_PUBLIC_APP_URL],
              allowedAssistantIds: [assistant.vapiId],
              allowTransientAssistant: false,
            },
          },
        }
        // Get the private key from environment variables
        const key = new TextEncoder().encode(process.env.VAPI_PRIVATE_KEY)

        // Generate the token using a JWT library or built-in functionality
        const token = await new SignJWT(payload)
          .setProtectedHeader({ alg: 'HS256' })
          .setExpirationTime('1h')
          .sign(key)
The JWT validates correctly using the private key, however from the vapi api we receive:
Copy code
{
  "message": "Couldn't verify JWT.",
  "error": "Unauthorized",
  "statusCode": 401
}
s
ccing: @Kings_big💫 Can you please help him ?
k
The 401 error may be due to an incorrect authorization header format, expired token, or signature mismatch, ensure your JWT is correctly formatted with the Bearer prefix, not expired, and signed with the correct key and algorithm..
2 Views