You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: fern/customization/jwt-authentication.mdx
+71-11Lines changed: 71 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,20 +23,66 @@ The following steps outline how to generate a JWT token:
23
23
3.**Set Token Options**: Define options for the token, such as the expiration time (`expiresIn`).
24
24
4.**Generate the Token**: Use a JWT library or built-in functionality to generate the token with the payload, key, and options.
25
25
26
-
### Example
26
+
### JWT Token Scopes
27
+
28
+
The generated JWT token can have one of two scopes: `private` or `public`. The scope of the token will determine the actions that can be performed using the token.
29
+
30
+
For example, it can be used to restrict which API endpoints the token can access.
31
+
32
+
<Note>
33
+
As of writing, the only publicly scoped API endpoint is
34
+
https://api.vapi.ai//call/web, which is used for Web Call creation. All other
35
+
endpoints are privately scoped.
36
+
</Note>
37
+
38
+
### Example (generating a private JWT token)
39
+
40
+
```js
41
+
// Define the payload
42
+
constpayload= {
43
+
orgId:process.env.ORG_ID,
44
+
token: {
45
+
// This is the scope of the token
46
+
tag:"private",
47
+
},
48
+
};
49
+
50
+
// Get the private key from environment variables
51
+
constkey=process.env.PRIVATE_KEY;
52
+
53
+
// Define token options
54
+
constoptions= {
55
+
expiresIn:"1h",
56
+
};
57
+
58
+
// Generate the token using a JWT library or built-in functionality
-**Payload**: The payload includes the `orgId`, representing the organization ID.
94
+
-**Payload**: The payload includes the `orgId` representing the organization ID and the `token` object with the scope of the token.
49
95
-**Key**: The private key is used to sign the token, ensuring its authenticity.
50
96
-**Options**: The `expiresIn` option specifies that the token will expire in 1 hour.
51
97
-**Token Generation**: The `generateJWT` function (a placeholder for the actual JWT generation method) creates the token using the provided payload, key, and options.
52
98
53
-
## Making an Authenticated API Request
99
+
## Usage (Making an Authenticated API Request)
54
100
55
-
Once the token is generated, you can use it to make authenticated API requests. The following steps outline how to make an authenticated request:
101
+
If you set the scope to `private`, you can use it to make authenticated API requests. The following steps outline how to make an authenticated request:
56
102
57
103
1.**Define the API Endpoint**: Specify the URL of the API you want to call.
58
104
2.**Set the Headers**: Include the `Content-Type` and `Authorization` headers in your request. The `Authorization` header should include the generated JWT token prefixed with `Bearer`.
@@ -62,10 +108,10 @@ Once the token is generated, you can use it to make authenticated API requests.
-**Headers**: The `Content-Type` is set to `application/json`, and the `Authorization` header includes the generated JWT token.
85
130
-**API Call**: The `fetchData` function makes an asynchronous GET request to the specified API endpoint and logs the response.
86
131
87
-
### Usage
132
+
### Usage (Web Client)
133
+
134
+
If you set the scope to `public`, you can use it to make authenticated API requests using the Vapi Web Client.
135
+
136
+
```
137
+
import Vapi from '@vapi-ai/web';
138
+
139
+
const vapi = new Vapi({
140
+
token: 'your-jwt-token',
141
+
});
142
+
143
+
vapi.start('your-assistant-id');
144
+
```
145
+
146
+
## Notes
88
147
89
-
With the generated token, you can authenticate API requests to any endpoint requiring authentication. The token will be valid for the duration specified in the options (1 hour in this case).
148
+
- With the generated token, you can authenticate API requests to any endpoint requiring authentication. The token will be valid for the duration specified in the options (1 hour in this case).
149
+
- If you don't specify `token` in the JWT payload, the token will be public.
0 commit comments