Skip to content

Commit a0b82ae

Browse files
update jwt docs (#266)
* update * update * fix * update * fixing * fixing * fixing * fixing * fixing * fixing * cool --------- Co-authored-by: Nikhil Gupta <[email protected]>
1 parent 483dd24 commit a0b82ae

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

fern/customization/jwt-authentication.mdx

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,66 @@ The following steps outline how to generate a JWT token:
2323
3. **Set Token Options**: Define options for the token, such as the expiration time (`expiresIn`).
2424
4. **Generate the Token**: Use a JWT library or built-in functionality to generate the token with the payload, key, and options.
2525

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+
const payload = {
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+
const key = process.env.PRIVATE_KEY;
52+
53+
// Define token options
54+
const options = {
55+
expiresIn: "1h",
56+
};
57+
58+
// Generate the token using a JWT library or built-in functionality
59+
const token = generateJWT(payload, key, options);
60+
```
61+
62+
### Example (generating a public JWT token)
2763

2864
```js
2965
// Define the payload
3066
const payload = {
3167
orgId: process.env.ORG_ID,
68+
// This is the scope of the token
69+
token: {
70+
tag: "public",
71+
restrictions: {
72+
enabled: true,
73+
allowedOrigins: ["https://example.vapi.ai"],
74+
allowedAssistantIds: ["1cbf8c70-5fd7-4f61-a220-376ab35be1b0"],
75+
allowTransientAssistant: false,
76+
},
77+
},
3278
};
3379

3480
// Get the private key from environment variables
3581
const key = process.env.PRIVATE_KEY;
3682

3783
// Define token options
3884
const options = {
39-
expiresIn: '1h',
85+
expiresIn: "1h",
4086
};
4187

4288
// Generate the token using a JWT library or built-in functionality
@@ -45,14 +91,14 @@ const token = generateJWT(payload, key, options);
4591

4692
### Explanation
4793

48-
- **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.
4995
- **Key**: The private key is used to sign the token, ensuring its authenticity.
5096
- **Options**: The `expiresIn` option specifies that the token will expire in 1 hour.
5197
- **Token Generation**: The `generateJWT` function (a placeholder for the actual JWT generation method) creates the token using the provided payload, key, and options.
5298

53-
## Making an Authenticated API Request
99+
## Usage (Making an Authenticated API Request)
54100

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:
56102

57103
1. **Define the API Endpoint**: Specify the URL of the API you want to call.
58104
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.
62108

63109
```js
64110
async function getAssistants() {
65-
const response = await fetch('https://api.vapi.ai/assistant', {
66-
method: 'GET',
111+
const response = await fetch("https://api.vapi.ai/assistant", {
112+
method: "GET",
67113
headers: {
68-
'Content-Type': 'application/json',
114+
"Content-Type": "application/json",
69115
Authorization: `Bearer ${token}`,
70116
},
71117
});
@@ -75,7 +121,6 @@ async function getAssistants() {
75121
}
76122

77123
fetchData().catch(console.error);
78-
79124
```
80125

81126
### Explanation
@@ -84,9 +129,24 @@ fetchData().catch(console.error);
84129
- **Headers**: The `Content-Type` is set to `application/json`, and the `Authorization` header includes the generated JWT token.
85130
- **API Call**: The `fetchData` function makes an asynchronous GET request to the specified API endpoint and logs the response.
86131

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
88147

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.
90150

91151
## Conclusion
92152

251 KB
Loading

0 commit comments

Comments
 (0)