|
1 | 1 | --- |
2 | | -title: Overview & Authentication |
3 | | -sidebarTitle: Overview & Auth |
4 | | -description: Using the Trigger.dev SDK from your frontend application. |
| 2 | +title: Authentication |
| 3 | +sidebarTitle: Authentication |
| 4 | +description: Authenticating real-time API requests with Public Access Tokens |
5 | 5 | --- |
6 | 6 |
|
7 | | -You can use our [React hooks](/frontend/react-hooks) in your frontend application to interact with the Trigger.dev API. This guide will show you how to generate Public Access Tokens that can be used to authenticate your requests. |
| 7 | +To use the Realtime API, you need to authenticate your requests with Public Access Tokens. These tokens provide secure, scoped access to your runs and can be used in both frontend and backend applications. |
8 | 8 |
|
9 | | -## Authentication |
| 9 | +## Creating Public Access Tokens |
10 | 10 |
|
11 | | -To create a Public Access Token, you can use the `auth.createPublicToken` function in your **backend** code: |
| 11 | +You can create a Public Access Token using the `auth.createPublicToken` function in your **backend** code: |
12 | 12 |
|
13 | 13 | ```tsx |
14 | 14 | import { auth } from "@trigger.dev/sdk/v3"; |
15 | 15 |
|
16 | 16 | const publicToken = await auth.createPublicToken(); // 👈 this public access token has no permissions, so is pretty useless! |
17 | 17 | ``` |
18 | 18 |
|
19 | | -### Scopes |
| 19 | +## Scopes |
20 | 20 |
|
21 | 21 | By default a Public Access Token has no permissions. You must specify the scopes you need when creating a Public Access Token: |
22 | 22 |
|
@@ -103,7 +103,7 @@ const publicToken = await auth.createPublicToken({ |
103 | 103 | }); |
104 | 104 | ``` |
105 | 105 |
|
106 | | -### Expiration |
| 106 | +## Expiration |
107 | 107 |
|
108 | 108 | By default, Public Access Token's expire after 15 minutes. You can specify a different expiration time when creating a Public Access Token: |
109 | 109 |
|
@@ -166,6 +166,78 @@ const handle = await tasks.batchTrigger("my-task", [ |
166 | 166 | console.log(handle.publicAccessToken); |
167 | 167 | ``` |
168 | 168 |
|
169 | | -## Usage |
| 169 | +## Trigger tokens |
170 | 170 |
|
171 | | -To learn how to use these Public Access Tokens, see our [React hooks](/frontend/react-hooks) guide. |
| 171 | +For triggering tasks from your frontend, you need special "trigger" tokens. These tokens can only be used once to trigger a task and are more secure than regular Public Access Tokens. |
| 172 | + |
| 173 | +```ts |
| 174 | +import { auth } from "@trigger.dev/sdk/v3"; |
| 175 | + |
| 176 | +// Somewhere in your backend code |
| 177 | +const triggerToken = await auth.createTriggerPublicToken("my-task"); |
| 178 | +``` |
| 179 | + |
| 180 | +These tokens also expire, with the default expiration time being 15 minutes. You can specify a custom expiration time: |
| 181 | + |
| 182 | +```ts |
| 183 | +import { auth } from "@trigger.dev/sdk/v3"; |
| 184 | + |
| 185 | +// Somewhere in your backend code |
| 186 | +const triggerToken = await auth.createTriggerPublicToken("my-task", { |
| 187 | + expirationTime: "24hr", |
| 188 | +}); |
| 189 | +``` |
| 190 | + |
| 191 | +You can pass multiple tasks to create a token that can trigger multiple tasks: |
| 192 | + |
| 193 | +```ts |
| 194 | +import { auth } from "@trigger.dev/sdk/v3"; |
| 195 | + |
| 196 | +// Somewhere in your backend code |
| 197 | +const triggerToken = await auth.createTriggerPublicToken(["my-task-1", "my-task-2"]); |
| 198 | +``` |
| 199 | + |
| 200 | +You can also create tokens that can be used multiple times: |
| 201 | + |
| 202 | +```ts |
| 203 | +import { auth } from "@trigger.dev/sdk/v3"; |
| 204 | + |
| 205 | +// Somewhere in your backend code |
| 206 | +const triggerToken = await auth.createTriggerPublicToken("my-task", { |
| 207 | + multipleUse: true, // ❌ Use this with caution! |
| 208 | +}); |
| 209 | +``` |
| 210 | + |
| 211 | +## Using tokens |
| 212 | + |
| 213 | +Once you have a Public Access Token, you can use it to authenticate requests to the Realtime API: |
| 214 | + |
| 215 | +### Backend usage |
| 216 | + |
| 217 | +```ts |
| 218 | +import { runs } from "@trigger.dev/sdk/v3"; |
| 219 | + |
| 220 | +// Using the token with backend functions |
| 221 | +for await (const run of runs.subscribeToRun("run_1234", { |
| 222 | + accessToken: publicToken, |
| 223 | +})) { |
| 224 | + console.log(run); |
| 225 | +} |
| 226 | +``` |
| 227 | + |
| 228 | +### Frontend usage |
| 229 | + |
| 230 | +```tsx |
| 231 | +import { useRealtimeRun } from "@trigger.dev/react-hooks"; |
| 232 | + |
| 233 | +export function MyComponent({ runId, publicAccessToken }) { |
| 234 | + const { run, error } = useRealtimeRun(runId, { |
| 235 | + accessToken: publicAccessToken, |
| 236 | + }); |
| 237 | + |
| 238 | + if (error) return <div>Error: {error.message}</div>; |
| 239 | + return <div>Run: {run?.id}</div>; |
| 240 | +} |
| 241 | +``` |
| 242 | + |
| 243 | +See our [React hooks documentation](/realtime/react-hooks) for more details on using hooks in your frontend application. |
0 commit comments