Waitpoint tokens pause task runs until you complete the token. They’re commonly used for approval workflows and other scenarios where you need to wait for external confirmation, such as human-in-the-loop processes.You can complete a token using the SDK or by making a POST request to the token’s URL.
This feature is only available in the v4 beta. To upgrade to v4, see the upgrade to v4
docs.
To get started using wait tokens, you need to first create a token using the wait.createToken function:
Copy
Ask AI
import { wait } from "@trigger.dev/sdk";// This can be called anywhere in your codebase, either in a task or in your backend codeconst token = await wait.createToken({ timeout: "10m", // you can optionally specify a timeout for the token});
Once you have a token, you can wait for it to be completed using the wait.forToken function:
Copy
Ask AI
import { wait } from "@trigger.dev/sdk";type ApprovalToken = { status: "approved" | "rejected";};// This must be called inside a task run functionconst result = await wait.forToken<ApprovalToken>(tokenId);if (result.ok) { console.log("Token completed", result.output.status); // "approved" or "rejected"} else { console.log("Token timed out", result.error);}
To complete a token, you can use the wait.completeToken function:
Copy
Ask AI
import { wait } from "@trigger.dev/sdk";// This can be called anywhere in your codebase, or from an external service,// passing in the token ID and the output of the tokenawait wait.completeToken<ApprovalToken>(tokenId, { status: "approved",});
Or you can make an HTTP POST request to the url it returns:
Copy
Ask AI
import { wait } from "@trigger.dev/sdk";const token = await wait.createToken({ timeout: "10m",});const call = await replicate.predictions.create({ version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478", input: { prompt: "A painting of a cat by Andy Warhol", }, // pass the provided URL to Replicate's webhook, so they can "callback" webhook: token.url, webhook_events_filter: ["completed"],});const prediction = await wait.forToken<Prediction>(token).unwrap();// unwrap() throws a timeout error or returns the result 👆
The URL of the token. This is the URL you can make a POST request to in order to complete the token.The JSON body of the POST request will be used as the output of the token. If there’s no body the output will be an empty object {}.
A Public Access Token that can be used to complete the token from a client-side application (or
another backend). See our frontend docs for more details.
If set to true, this will cause the waitpoint to release the current run from the queue’s concurrency.This is useful if you want to allow other runs to execute while waitingNote: It’s possible that this run will not be able to resume when the waitpoint is complete if this is set to true.
It will go back in the queue and will resume once concurrency becomes available.The default is false.
We provide a handy .unwrap() method that will throw an error if the result is not ok. This means your happy path is a lot cleaner.
Copy
Ask AI
const approval = await wait.forToken<ApprovalToken>(tokenId).unwrap();// unwrap means an error will throw if the waitpoint times out 👆// This is the actual data you sent to the token now, not a result objectconsole.log("Approval", approval);
The listTokens function returns a list of tokens that can be iterated over using a for-await-of loop.Each token is an object with the following properties:
The URL of the token. This is the URL you can make a POST request to in order to complete the token.The JSON body of the POST request will be used as the output of the token. If there’s no body the output will be an empty object {}.
The URL of the token. This is the URL you can make a POST request to in order to complete the token.The JSON body of the POST request will be used as the output of the token. If there’s no body the output will be an empty object {}.