Skip to content

Commit b87d98f

Browse files
committed
Updated the docs
1 parent 4e9c2ba commit b87d98f

File tree

1 file changed

+50
-59
lines changed

1 file changed

+50
-59
lines changed

docs/wait-for-http-callback.mdx

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,86 +5,82 @@ description: "Pause runs until an HTTP callback is made to the provided URL."
55

66
import UpgradeToV4Note from "/snippets/upgrade-to-v4-note.mdx";
77

8-
The `wait.forHttpCallback()` function gives you a callback URL, and then pauses the run until that callback is hit with a POST request. This is most commonly used with 3rd party APIs that take a long time and that accept a callback (or webhook) URL.
8+
The `wait.createHttpCallback()` function gives you a callback URL and returns a token representing the waitpoint. You should call a third-party API and provide it with the callback URL so they can notify you when their work is done.
99

10-
When the callback URL is requested the run will continue where it left off with the body of the request as the output available for you to use.
10+
Then you can use `wait.forToken()` to pause the run until the callback URL is hit with a `POST` request.
1111

1212
<UpgradeToV4Note />
1313

1414
## Usage
1515

16-
In this example we create an image using Replicate. Their API accepts a "webhook", which is a callback.
16+
In this example we create an image using Replicate. Their API accepts a webhook, which is an HTTP POST callback.
1717

1818
```ts
1919
import { logger, task, wait } from "@trigger.dev/sdk";
20-
21-
const replicate = new Replicate({
22-
auth: process.env.REPLICATE_API_KEY,
23-
});
20+
import Replicate, { Prediction } from "replicate";
2421

2522
export const replicate = task({
2623
id: "replicate",
2724
run: async () => {
28-
// This will pause the run and give you a URL
29-
const result = await wait.forHttpCallback<Prediction>(
25+
const replicate = new Replicate({
26+
auth: process.env.REPLICATE_API_KEY,
27+
});
28+
29+
// 1️⃣ Create the callback URL and token
30+
const { token, data } = await wait.createHttpCallback(
3031
async (url) => {
3132
// 👆 This URL continues your run when hit with a POST request
3233

33-
// Make the call to Replicate, passing in the URL
34-
await replicate.predictions.create({
34+
// 2️⃣ Kick off the long-running job, passing in the URL
35+
return replicate.predictions.create({
3536
version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
3637
input: {
3738
prompt: "A painting of a cat by Any Warhol",
3839
},
39-
// Make sure to pass the callback URL
40-
// 👇
41-
webhook: url,
42-
// We only want to call when it's completed
40+
webhook: url, // 👈 pass the callback URL
4341
webhook_events_filter: ["completed"],
4442
});
4543
},
4644
{
4745
// We'll fail the waitpoint after 10m of inactivity
4846
timeout: "10m",
47+
// Tags can be used to filter waitpoints in the dashboard
48+
tags: ["replicate"],
4949
}
5050
);
5151

52-
if (!result.ok) {
52+
logger.log("Create result", { token, data });
53+
// What you returned from the callback 👆
54+
55+
// 3️⃣ Wait for the callback to be received
56+
const prediction = await wait.forToken<Prediction>(token);
57+
if (!prediction.ok) {
5358
throw new Error("Failed to create prediction");
5459
}
5560

56-
logger.log("Result", result);
61+
logger.log("Prediction", prediction);
5762

58-
const imageUrl = result.output.output;
63+
const imageUrl = prediction.output.output;
5964
logger.log("Image URL", imageUrl);
6065
},
6166
});
6267
```
6368

6469
## unwrap()
6570

66-
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.
71+
`wait.forToken()` also supports `.unwrap()` which throws if the waitpoint times out,
72+
keeping the happy path clean:
6773

6874
```ts
69-
const prediction = await wait
70-
.forHttpCallback<Prediction>(
71-
async (url) => {
72-
// ...
73-
},
74-
{
75-
timeout: "10m",
76-
}
77-
)
78-
.unwrap();
79-
// 👆 This will throw an error if the waitpoint times out
75+
const prediction = await wait.forToken<Prediction>(token).unwrap();
8076

81-
// This is the actual data you sent to the callback now, not a result object
77+
// This is the result data that Replicate sent back (via HTTP POST)
8278
logger.log("Prediction", prediction);
8379
```
8480

8581
### Options
8682

87-
The `wait.forHttpCallback` function accepts an optional second parameter with the following properties:
83+
The `wait.createHttpCallback()` function accepts an optional second parameter with the following properties:
8884

8985
<ParamField query="timeout" type="string" optional>
9086
The maximum amount of time to wait for the token to be completed.
@@ -104,41 +100,36 @@ The `wait.forHttpCallback` function accepts an optional second parameter with th
104100
Tags to attach to the token. Tags can be used to filter waitpoints in the dashboard.
105101
</ParamField>
106102

107-
<ParamField query="releaseConcurrency" type="boolean" optional>
108-
If set to true, this will cause the waitpoint to release the current run from the queue's concurrency.
109-
110-
This is useful if you want to allow other runs to execute while waiting
111-
112-
Note: It's possible that this run will not be able to resume when the waitpoint is complete if this is set to true.
113-
It will go back in the queue and will resume once concurrency becomes available.
114-
115-
The default is `false`.
116-
117-
</ParamField>
118-
119103
### returns
120104

121-
The `forHttpCallback` function returns a result object with the following properties:
105+
`wait.createHttpCallback()` returns an object with:
122106

123-
<ParamField query="ok" type="boolean">
107+
<ParamField query="token" type="object">
124108
Whether the token was completed successfully.
125-
</ParamField>
126109

127-
<ParamField query="output" type="any">
128-
If `ok` is `true`, this will be the output of the token.
110+
<Expandable title="properties" defaultOpen={true}>
111+
<ParamField query="id" type="string">
112+
The ID of the token. Starts with `waitpoint_`.
113+
</ParamField>
114+
<ParamField query="isCached" type="boolean">
115+
Whether the token is cached. Will return true if the token was created with an idempotency key and
116+
the same idempotency key was used again.
117+
</ParamField>
118+
119+
<ParamField query="url" type="string">
120+
The URL that was created for the waitpoint. Call this via an HTTP POST request to complete the
121+
waitpoint and continue the run with the JSON body of the request.
122+
</ParamField>
123+
124+
</Expandable>
129125
</ParamField>
130126

131-
<ParamField query="error" type="Error">
132-
If `ok` is `false`, this will be the error that occurred. The only error that can occur is a
133-
timeout error.
127+
<ParamField query="data" type="object">
128+
If you returned anything from the function, it will be here.
134129
</ParamField>
135130

136-
### unwrap() returns
137-
138-
If you use the `unwrap()` method, it will just return the output of the token. If an error occurs it will throw an error.
139-
140-
## The format of the callback request
131+
## Calling the callback URL yourself
141132

142-
If you want to offload work to one of your own services you will need to do the callback request yourself.
133+
`wait.createHttpCallback()` returns a unique one-time-use URL.
143134

144-
The `wait.forHttpCallback()` function gives you a unique one-time use URL. You should do a `POST` request to this URL with a text body that is JSON parseable.
135+
You should do a `POST` request to this URL with a text body that is JSON-parseable. If there's no body it will use an empty object `{}`.

0 commit comments

Comments
 (0)