|
| 1 | +--- |
| 2 | +title: Overview & Authentication |
| 3 | +sidebarTitle: Overview & Auth |
| 4 | +description: Using the Trigger.dev SDK from your frontend application. |
| 5 | +--- |
| 6 | + |
| 7 | +You can use certain SDK functions in your frontend application to interact with the Trigger.dev API. This guide will show you how to authenticate your requests and use the SDK in your frontend application. |
| 8 | + |
| 9 | +## Authentication |
| 10 | + |
| 11 | +You must authenticate your requests using a "Public Access Token" when using the SDK in your frontend application. To create a Public Access Token, you can use the `auth.createPublicToken` function in your backend code: |
| 12 | + |
| 13 | +```tsx |
| 14 | +const publicToken = await auth.createPublicToken(); |
| 15 | +``` |
| 16 | + |
| 17 | +To use a Public Access Token in your frontend application, you can call the `auth.configure` function or the `auth.withAuth` function: |
| 18 | + |
| 19 | +```ts |
| 20 | +import { auth } from "@trigger.dev/sdk/v3"; |
| 21 | + |
| 22 | +auth.configure({ |
| 23 | + accessToken: publicToken, |
| 24 | +}); |
| 25 | + |
| 26 | +// or |
| 27 | +await auth.withAuth({ accessToken: publicToken }, async () => { |
| 28 | + // Your code here will use the public token |
| 29 | +}); |
| 30 | +``` |
| 31 | + |
| 32 | +### Scopes |
| 33 | + |
| 34 | +By default a Public Access Token has limited permissions. You can specify the scopes you need when creating a Public Access Token: |
| 35 | + |
| 36 | +```ts |
| 37 | +const publicToken = await auth.createPublicToken({ |
| 38 | + scopes: { |
| 39 | + read: { |
| 40 | + runs: true, |
| 41 | + }, |
| 42 | + }, |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +This will allow the token to read all runs, which is probably not what you want. You can specify only certain runs by passing an array of run IDs: |
| 47 | + |
| 48 | +```ts |
| 49 | +const publicToken = await auth.createPublicToken({ |
| 50 | + scopes: { |
| 51 | + read: { |
| 52 | + runs: ["run_1234", "run_5678"], |
| 53 | + }, |
| 54 | + }, |
| 55 | +}); |
| 56 | +``` |
| 57 | + |
| 58 | +You can scope the token to only read certain tasks: |
| 59 | + |
| 60 | +```ts |
| 61 | +const publicToken = await auth.createPublicToken({ |
| 62 | + scopes: { |
| 63 | + read: { |
| 64 | + tasks: ["my-task-1", "my-task-2"], |
| 65 | + }, |
| 66 | + }, |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +Or tags: |
| 71 | + |
| 72 | +```ts |
| 73 | +const publicToken = await auth.createPublicToken({ |
| 74 | + scopes: { |
| 75 | + read: { |
| 76 | + tags: ["my-tag-1", "my-tag-2"], |
| 77 | + }, |
| 78 | + }, |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +Or a specific batch of runs: |
| 83 | + |
| 84 | +```ts |
| 85 | +const publicToken = await auth.createPublicToken({ |
| 86 | + scopes: { |
| 87 | + read: { |
| 88 | + batch: "batch_1234", |
| 89 | + }, |
| 90 | + }, |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +You can also combine scopes. For example, to read only certain tasks and tags: |
| 95 | + |
| 96 | +```ts |
| 97 | +const publicToken = await auth.createPublicToken({ |
| 98 | + scopes: { |
| 99 | + read: { |
| 100 | + tasks: ["my-task-1", "my-task-2"], |
| 101 | + tags: ["my-tag-1", "my-tag-2"], |
| 102 | + }, |
| 103 | + }, |
| 104 | +}); |
| 105 | +``` |
| 106 | + |
| 107 | +### Expiration |
| 108 | + |
| 109 | +By default, Public Access Token's expire after 15 minutes. You can specify a different expiration time when creating a Public Access Token: |
| 110 | + |
| 111 | +```ts |
| 112 | +const publicToken = await auth.createPublicToken({ |
| 113 | + expirationTime: "1hr", |
| 114 | +}); |
| 115 | +``` |
| 116 | + |
| 117 | +- If `expirationTime` is a string, it will be treated as a time span |
| 118 | +- If `expirationTime` is a number, it will be treated as a Unix timestamp |
| 119 | +- If `expirationTime` is a `Date`, it will be treated as a date |
| 120 | + |
| 121 | +The format used for a time span is the same as the [jose package](https://github.com/panva/jose), which is a number followed by a unit. Valid units are: "sec", "secs", "second", "seconds", "s", "minute", "minutes", "min", "mins", "m", "hour", "hours", "hr", "hrs", "h", "day", "days", "d", "week", "weeks", "w", "year", "years", "yr", "yrs", and "y". It is not possible to specify months. 365.25 days is used as an alias for a year. If the string is suffixed with "ago", or prefixed with a "-", the resulting time span gets subtracted from the current unix timestamp. A "from now" suffix can also be used for readability when adding to the current unix timestamp. |
| 122 | + |
| 123 | +## Auto-generated tokens |
| 124 | + |
| 125 | +When triggering a task from your backend, the `handle` received from the `trigger` function now includes a `publicAccessToken` field. This token can be used to authenticate requests in your frontend application: |
| 126 | + |
| 127 | +```ts |
| 128 | +import { tasks } from "@trigger.dev/sdk/v3"; |
| 129 | + |
| 130 | +const handle = await tasks.trigger("my-task", { some: "data" }); |
| 131 | + |
| 132 | +console.log(handle.publicAccessToken); |
| 133 | +``` |
| 134 | + |
| 135 | +By default, tokens returned from the `trigger` function expire after 15 minutes and have a read scope for that specific run, and any tags associated with it. You can customize the expiration of the auto-generated tokens by passing a `publicTokenOptions` object to the `trigger` function: |
| 136 | + |
| 137 | +```ts |
| 138 | +const handle = await tasks.trigger( |
| 139 | + "my-task", |
| 140 | + { some: "data" }, |
| 141 | + { |
| 142 | + tags: ["my-tag"], |
| 143 | + }, |
| 144 | + { |
| 145 | + publicAccessToken: { |
| 146 | + expirationTime: "1hr", |
| 147 | + }, |
| 148 | + } |
| 149 | +); |
| 150 | +``` |
| 151 | + |
| 152 | +You will also get back a Public Access Token when using the `batchTrigger` function: |
| 153 | + |
| 154 | +```ts |
| 155 | +import { tasks } from "@trigger.dev/sdk/v3"; |
| 156 | + |
| 157 | +const handle = await tasks.batchTrigger("my-task", [ |
| 158 | + { payload: { some: "data" } }, |
| 159 | + { payload: { some: "data" } }, |
| 160 | + { payload: { some: "data" } }, |
| 161 | +]); |
| 162 | + |
| 163 | +console.log(handle.publicAccessToken); |
| 164 | +``` |
| 165 | + |
| 166 | +## Available SDK functions |
| 167 | + |
| 168 | +Currently the following functions are available in the frontend SDK: |
| 169 | + |
| 170 | +### runs.retrieve |
| 171 | + |
| 172 | +The `runs.retrieve` function allows you to retrieve a run by its ID. |
| 173 | + |
| 174 | +```ts |
| 175 | +import { runs, auth } from "@trigger.dev/sdk/v3"; |
| 176 | + |
| 177 | +// Somewhere in your backend code |
| 178 | +const handle = await tasks.trigger("my-task", { some: "data" }); |
| 179 | + |
| 180 | +// In your frontend code |
| 181 | +auth.configure({ |
| 182 | + accessToken: handle.publicAccessToken, |
| 183 | +}); |
| 184 | + |
| 185 | +const run = await runs.retrieve(handle.id); |
| 186 | +``` |
| 187 | + |
| 188 | +Learn more about the `runs.retrieve` function in the [runs.retrieve doc](/management/runs/retrieve). |
| 189 | + |
| 190 | +### runs.subscribeToRun |
| 191 | + |
| 192 | +The `runs.subscribeToRun` function allows you to subscribe to a run by its ID, and receive updates in real-time when the run changes. |
| 193 | + |
| 194 | +```ts |
| 195 | +import { runs, auth } from "@trigger.dev/sdk/v3"; |
| 196 | + |
| 197 | +// Somewhere in your backend code |
| 198 | +const handle = await tasks.trigger("my-task", { some: "data" }); |
| 199 | + |
| 200 | +// In your frontend code |
| 201 | +auth.configure({ |
| 202 | + accessToken: handle.publicAccessToken, |
| 203 | +}); |
| 204 | + |
| 205 | +for await (const run of runs.subscribeToRun(handle.id)) { |
| 206 | + // This will log the run every time it changes |
| 207 | + console.log(run); |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +See the [Realtime doc](/realtime) for more information. |
| 212 | + |
| 213 | +### runs.subscribeToRunsWithTag |
| 214 | + |
| 215 | +The `runs.subscribeToRunsWithTag` function allows you to subscribe to runs with a specific tag, and receive updates in real-time when the runs change. |
| 216 | + |
| 217 | +```ts |
| 218 | +import { runs, auth } from "@trigger.dev/sdk/v3"; |
| 219 | + |
| 220 | +// Somewhere in your backend code |
| 221 | +const handle = await tasks.trigger("my-task", { some: "data" }, { tags: ["my-tag"] }); |
| 222 | + |
| 223 | +// In your frontend code |
| 224 | +auth.configure({ |
| 225 | + accessToken: handle.publicAccessToken, |
| 226 | +}); |
| 227 | + |
| 228 | +for await (const run of runs.subscribeToRunsWithTag("my-tag")) { |
| 229 | + // This will log the run every time it changes |
| 230 | + console.log(run); |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +See the [Realtime doc](/realtime) for more information. |
| 235 | + |
| 236 | +## React hooks |
| 237 | + |
| 238 | +We also provide React hooks to make it easier to use the SDK in your React application. See our [React hooks](/frontend/react-hooks) documentation for more information. |
| 239 | + |
| 240 | +## Triggering tasks |
| 241 | + |
| 242 | +We don't currently support triggering tasks from the frontend SDK. If this is something you need, please let us know by [upvoting the feature](https://feedback.trigger.dev/p/ability-to-trigger-tasks-from-frontend). |
0 commit comments