Skip to content

Commit c52d986

Browse files
committed
WIP
1 parent 70325e4 commit c52d986

File tree

3 files changed

+256
-8
lines changed

3 files changed

+256
-8
lines changed

docs/frontend/overview.mdx

Lines changed: 238 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,244 @@
11
---
22
title: Overview & Authentication
33
sidebarTitle: Overview & Auth
4-
description: Using the Trigger.dev v3 API from your frontend application.
4+
description: Using the Trigger.dev SDK from your frontend application.
55
---
66

77
Install the preview package: `0.0.0-realtime-20241021152316`.
8+
9+
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.
10+
11+
## Authentication
12+
13+
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:
14+
15+
```tsx
16+
const publicToken = await auth.createPublicToken();
17+
```
18+
19+
To use a Public Access Token in your frontend application, you can call the `auth.configure` function or the `auth.withAuth` function:
20+
21+
```ts
22+
import { auth } from "@trigger.dev/sdk/v3";
23+
24+
auth.configure({
25+
accessToken: publicToken,
26+
});
27+
28+
// or
29+
await auth.withAuth({ accessToken: publicToken }, async () => {
30+
// Your code here will use the public token
31+
});
32+
```
33+
34+
### Scopes
35+
36+
By default a Public Access Token has limited permissions. You can specify the scopes you need when creating a Public Access Token:
37+
38+
```ts
39+
const publicToken = await auth.createPublicToken({
40+
scopes: {
41+
read: {
42+
runs: true,
43+
},
44+
},
45+
});
46+
```
47+
48+
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:
49+
50+
```ts
51+
const publicToken = await auth.createPublicToken({
52+
scopes: {
53+
read: {
54+
runs: ["run_1234", "run_5678"],
55+
},
56+
},
57+
});
58+
```
59+
60+
You can scope the token to only read certain tasks:
61+
62+
```ts
63+
const publicToken = await auth.createPublicToken({
64+
scopes: {
65+
read: {
66+
tasks: ["my-task-1", "my-task-2"],
67+
},
68+
},
69+
});
70+
```
71+
72+
Or tags:
73+
74+
```ts
75+
const publicToken = await auth.createPublicToken({
76+
scopes: {
77+
read: {
78+
tags: ["my-tag-1", "my-tag-2"],
79+
},
80+
},
81+
});
82+
```
83+
84+
Or a specific batch of runs:
85+
86+
```ts
87+
const publicToken = await auth.createPublicToken({
88+
scopes: {
89+
read: {
90+
batch: "batch_1234",
91+
},
92+
},
93+
});
94+
```
95+
96+
You can also combine scopes. For example, to read only certain tasks and tags:
97+
98+
```ts
99+
const publicToken = await auth.createPublicToken({
100+
scopes: {
101+
read: {
102+
tasks: ["my-task-1", "my-task-2"],
103+
tags: ["my-tag-1", "my-tag-2"],
104+
},
105+
},
106+
});
107+
```
108+
109+
### Expiration
110+
111+
By default, Public Access Token's expire after 15 minutes. You can specify a different expiration time when creating a Public Access Token:
112+
113+
```ts
114+
const publicToken = await auth.createPublicToken({
115+
expirationTime: "1hr",
116+
});
117+
```
118+
119+
- If `expirationTime` is a string, it will be treated as a time span
120+
- If `expirationTime` is a number, it will be treated as a Unix timestamp
121+
- If `expirationTime` is a `Date`, it will be treated as a date
122+
123+
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.
124+
125+
## Auto-generated tokens
126+
127+
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:
128+
129+
```ts
130+
import { tasks } from "@trigger.dev/sdk/v3";
131+
132+
const handle = await tasks.trigger("my-task", { some: "data" });
133+
134+
console.log(handle.publicAccessToken);
135+
```
136+
137+
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:
138+
139+
```ts
140+
const handle = await tasks.trigger(
141+
"my-task",
142+
{ some: "data" },
143+
{
144+
tags: ["my-tag"],
145+
},
146+
{
147+
publicAccessToken: {
148+
expirationTime: "1hr",
149+
},
150+
}
151+
);
152+
```
153+
154+
You will also get back a Public Access Token when using the `batchTrigger` function:
155+
156+
```ts
157+
import { tasks } from "@trigger.dev/sdk/v3";
158+
159+
const handle = await tasks.batchTrigger("my-task", [
160+
{ payload: { some: "data" } },
161+
{ payload: { some: "data" } },
162+
{ payload: { some: "data" } },
163+
]);
164+
165+
console.log(handle.publicAccessToken);
166+
```
167+
168+
## Available SDK functions
169+
170+
Currently the following functions are available in the frontend SDK:
171+
172+
### runs.retrieve
173+
174+
The `runs.retrieve` function allows you to retrieve a run by its ID.
175+
176+
```ts
177+
import { runs, auth } from "@trigger.dev/sdk/v3";
178+
179+
// Somewhere in your backend code
180+
const handle = await tasks.trigger("my-task", { some: "data" });
181+
182+
// In your frontend code
183+
auth.configure({
184+
accessToken: handle.publicAccessToken,
185+
});
186+
187+
const run = await runs.retrieve(handle.id);
188+
```
189+
190+
Learn more about the `runs.retrieve` function in the [runs.retrieve doc](/management/runs/retrieve).
191+
192+
### runs.subscribeToRun
193+
194+
The `runs.subscribeToRun` function allows you to subscribe to a run by its ID, and receive updates in real-time when the run changes.
195+
196+
```ts
197+
import { runs, auth } from "@trigger.dev/sdk/v3";
198+
199+
// Somewhere in your backend code
200+
const handle = await tasks.trigger("my-task", { some: "data" });
201+
202+
// In your frontend code
203+
auth.configure({
204+
accessToken: handle.publicAccessToken,
205+
});
206+
207+
for await (const run of runs.subscribeToRun(handle.id)) {
208+
// This will log the run every time it changes
209+
console.log(run);
210+
}
211+
```
212+
213+
See the [Realtime doc](/realtime) for more information.
214+
215+
### runs.subscribeToRunsWithTag
216+
217+
The `runs.subscribeToRunsWithTag` function allows you to subscribe to runs with a specific tag, and receive updates in real-time when the runs change.
218+
219+
```ts
220+
import { runs, auth } from "@trigger.dev/sdk/v3";
221+
222+
// Somewhere in your backend code
223+
const handle = await tasks.trigger("my-task", { some: "data" }, { tags: ["my-tag"] });
224+
225+
// In your frontend code
226+
auth.configure({
227+
accessToken: handle.publicAccessToken,
228+
});
229+
230+
for await (const run of runs.subscribeToRunsWithTag("my-tag")) {
231+
// This will log the run every time it changes
232+
console.log(run);
233+
}
234+
```
235+
236+
See the [Realtime doc](/realtime) for more information.
237+
238+
## React hooks
239+
240+
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.
241+
242+
## Triggering tasks
243+
244+
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).

docs/management/overview.mdx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ main().catch(console.error);
5050

5151
There are two methods of authenticating with the management API: using a secret key associated with a specific environment in a project (`secretKey`), or using a personal access token (`personalAccessToken`). Both methods should only be used in a backend server, as they provide full access to the project.
5252

53-
<Info>
54-
Support for client-side authentication is coming soon to v3 but is not available at the time of
55-
writing.
56-
</Info>
53+
<Note>
54+
There is a separate authentication strategy when making requests from your frontend application.
55+
See the [Frontend guide](/frontend/overview) for more information. This guide is for backend usage
56+
only.
57+
</Note>
5758

5859
Certain API functions work with both authentication methods, but require different arguments depending on the method used. For example, the `runs.list` function can be called using either a `secretKey` or a `personalAccessToken`, but the `projectRef` argument is required when using a `personalAccessToken`:
5960

references/v3-catalog/src/clientUsage.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { auth, runs, tasks } from "@trigger.dev/sdk/v3";
2-
import type { task1, task2 } from "./trigger/taskTypes.js";
2+
import type { task1, zodTask } from "./trigger/taskTypes.js";
33
import { randomUUID } from "crypto";
44

55
async function main() {
@@ -22,8 +22,18 @@ async function main() {
2222

2323
console.log("Auto JWT", anyHandle.publicAccessToken);
2424

25+
const publicToken = await auth.createPublicToken({
26+
scopes: {
27+
read: {
28+
runs: true,
29+
},
30+
},
31+
});
32+
2533
await auth.withAuth({ accessToken: anyHandle.publicAccessToken }, async () => {
26-
const subscription = runs.subscribeToRunsWithTag<typeof task1 | typeof task2>(`user:${userId}`);
34+
const subscription = runs.subscribeToRunsWithTag<typeof task1 | typeof zodTask>(
35+
`user:${userId}`
36+
);
2737

2838
for await (const run of subscription) {
2939
switch (run.taskIdentifier) {
@@ -33,7 +43,7 @@ async function main() {
3343
console.log("Payload:", run.payload);
3444
break;
3545
}
36-
case "types/task-2": {
46+
case "types/zod": {
3747
console.log("Run update:", run);
3848
console.log("Output:", run.output);
3949
console.log("Payload:", run.payload);

0 commit comments

Comments
 (0)