|
| 1 | +--- |
| 2 | +title: Authentication |
| 3 | +sidebarTitle: Authentication |
| 4 | +description: Authenticating with the Trigger.dev management API |
| 5 | +--- |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +<Note> |
| 10 | + There is a separate authentication strategy when making requests from your frontend application. |
| 11 | + See the [Frontend guide](/frontend/overview) for more information. This guide is for backend usage |
| 12 | + only. |
| 13 | +</Note> |
| 14 | + |
| 15 | +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`: |
| 16 | + |
| 17 | +```ts |
| 18 | +import { configure, runs } from "@trigger.dev/sdk/v3"; |
| 19 | + |
| 20 | +// Using secretKey authentication |
| 21 | +configure({ |
| 22 | + secretKey: process.env["TRIGGER_SECRET_KEY"], // starts with tr_dev_ or tr_prod_ |
| 23 | +}); |
| 24 | + |
| 25 | +function secretKeyExample() { |
| 26 | + return runs.list({ |
| 27 | + limit: 10, |
| 28 | + status: ["COMPLETED"], |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +// Using personalAccessToken authentication |
| 33 | +configure({ |
| 34 | + secretKey: process.env["TRIGGER_ACCESS_TOKEN"], // starts with tr_pat_ |
| 35 | +}); |
| 36 | + |
| 37 | +function personalAccessTokenExample() { |
| 38 | + // Notice the projectRef argument is required when using a personalAccessToken |
| 39 | + return runs.list("prof_1234", { |
| 40 | + limit: 10, |
| 41 | + status: ["COMPLETED"], |
| 42 | + projectRef: "tr_proj_1234567890", |
| 43 | + }); |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +<Accordion title="View endpoint support"> |
| 48 | + Consult the following table to see which endpoints support each authentication method. |
| 49 | + |
| 50 | +| Endpoint | Secret key | Personal Access Token | |
| 51 | +| ---------------------- | ---------- | --------------------- | |
| 52 | +| `task.trigger` | ✅ | | |
| 53 | +| `task.batchTrigger` | ✅ | | |
| 54 | +| `runs.list` | ✅ | ✅ | |
| 55 | +| `runs.retrieve` | ✅ | | |
| 56 | +| `runs.cancel` | ✅ | | |
| 57 | +| `runs.replay` | ✅ | | |
| 58 | +| `envvars.list` | ✅ | ✅ | |
| 59 | +| `envvars.retrieve` | ✅ | ✅ | |
| 60 | +| `envvars.upload` | ✅ | ✅ | |
| 61 | +| `envvars.create` | ✅ | ✅ | |
| 62 | +| `envvars.update` | ✅ | ✅ | |
| 63 | +| `envvars.del` | ✅ | ✅ | |
| 64 | +| `schedules.list` | ✅ | | |
| 65 | +| `schedules.create` | ✅ | | |
| 66 | +| `schedules.retrieve` | ✅ | | |
| 67 | +| `schedules.update` | ✅ | | |
| 68 | +| `schedules.activate` | ✅ | | |
| 69 | +| `schedules.deactivate` | ✅ | | |
| 70 | +| `schedules.del` | ✅ | | |
| 71 | + |
| 72 | +</Accordion> |
| 73 | + |
| 74 | +### Secret key |
| 75 | + |
| 76 | +Secret key authentication scopes the API access to a specific environment in a project, and works with certain endpoints. You can read our [API Keys guide](/apikeys) for more information. |
| 77 | + |
| 78 | +### Personal Access Token (PAT) |
| 79 | + |
| 80 | +A PAT is a token associated with a specific user, and gives access to all the orgs, projects, and environments that the user has access to. You can identify a PAT by the `tr_pat_` prefix. Because a PAT does not scope access to a specific environment, you must provide the `projectRef` argument when using a PAT (and sometimes the environment as well). |
| 81 | + |
| 82 | +For example, when uploading environment variables using a PAT, you must provide the `projectRef` and `environment` arguments: |
| 83 | + |
| 84 | +```ts |
| 85 | +import { configure, envvars } from "@trigger.dev/sdk/v3"; |
| 86 | + |
| 87 | +configure({ |
| 88 | + secretKey: process.env["TRIGGER_ACCESS_TOKEN"], // starts with tr_pat_ |
| 89 | +}); |
| 90 | + |
| 91 | +await envvars.upload("proj_1234", "dev", { |
| 92 | + variables: { |
| 93 | + MY_ENV_VAR: "MY_ENV_VAR_VALUE", |
| 94 | + }, |
| 95 | + override: true, |
| 96 | +}); |
| 97 | +``` |
0 commit comments