Skip to content

Commit d3fd26d

Browse files
committed
cool
1 parent 3677078 commit d3fd26d

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

fern/customization/jwt-authentication.mdx

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ The following steps outline how to generate a JWT token:
2525

2626
### JWT Token Scopes
2727

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

3030
For example, it can be used to restrict which API endpoints the token can access.
3131

3232
<Note>
33-
As of writing, the only publicly scoped API endpoint is https://api.vapi.ai//call/web, which is used for Web Call creation. All other endpoints are privately scoped.
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.
3436
</Note>
3537

3638
### Example (generating a private JWT token)
@@ -41,7 +43,7 @@ const payload = {
4143
orgId: process.env.ORG_ID,
4244
token: {
4345
// This is the scope of the token
44-
tag: 'private',
46+
tag: "private",
4547
},
4648
};
4749

@@ -50,7 +52,7 @@ const key = process.env.PRIVATE_KEY;
5052

5153
// Define token options
5254
const options = {
53-
expiresIn: '1h',
55+
expiresIn: "1h",
5456
};
5557

5658
// Generate the token using a JWT library or built-in functionality
@@ -63,13 +65,13 @@ const token = generateJWT(payload, key, options);
6365
// Define the payload
6466
const payload = {
6567
orgId: process.env.ORG_ID,
68+
// This is the scope of the token
6669
token: {
67-
// This is the scope of the token
68-
tag: 'public',
70+
tag: "public",
6971
restrictions: {
7072
enabled: true,
71-
allowedOrigins: ['https://example.vapi.ai'],
72-
allowedAssistantIds: ['1cbf8c70-5fd7-4f61-a220-376ab35be1b0'],
73+
allowedOrigins: ["https://example.vapi.ai"],
74+
allowedAssistantIds: ["1cbf8c70-5fd7-4f61-a220-376ab35be1b0"],
7375
allowTransientAssistant: false,
7476
},
7577
},
@@ -80,7 +82,7 @@ const key = process.env.PRIVATE_KEY;
8082

8183
// Define token options
8284
const options = {
83-
expiresIn: '1h',
85+
expiresIn: "1h",
8486
};
8587

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

9092
### Explanation
9193

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

97-
## Making an Authenticated API Request
99+
## Usage (Making an Authenticated API Request)
98100

99-
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:
100102

101103
1. **Define the API Endpoint**: Specify the URL of the API you want to call.
102104
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`.
@@ -106,10 +108,10 @@ Once the token is generated, you can use it to make authenticated API requests.
106108

107109
```js
108110
async function getAssistants() {
109-
const response = await fetch('https://api.vapi.ai/assistant', {
110-
method: 'GET',
111+
const response = await fetch("https://api.vapi.ai/assistant", {
112+
method: "GET",
111113
headers: {
112-
'Content-Type': 'application/json',
114+
"Content-Type": "application/json",
113115
Authorization: `Bearer ${token}`,
114116
},
115117
});
@@ -119,7 +121,6 @@ async function getAssistants() {
119121
}
120122

121123
fetchData().catch(console.error);
122-
123124
```
124125

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

131-
### 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
132147

133-
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.
134150

135151
## Conclusion
136152

0 commit comments

Comments
 (0)