Skip to content

Commit 49eb84a

Browse files
committed
Support webhooks when creating predictions
1 parent 90a1472 commit 49eb84a

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Install with `npm install replicate`
1414

1515
Set your API token as an environment variable called `REPLICATE_API_TOKEN`.
1616

17+
### Making preedictions
18+
1719
To run a prediction and return its output:
1820

1921
```js
@@ -73,6 +75,32 @@ console.log(prediction.status); // "starting"
7375
From there, you can fetch the current status of the prediction using
7476
`await prediction.load()` or `await replicate.prediction(prediction.id).load()`.
7577

78+
#### Webhooks
79+
80+
You can also provide webhook configuration to have Replicate send POST requests
81+
to your service when certain events occur:
82+
83+
```js
84+
import replicate from "replicate";
85+
86+
await replicate
87+
.model(
88+
"stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf"
89+
)
90+
.createPrediction(
91+
{
92+
prompt: "painting of a cat by andy warhol",
93+
},
94+
{
95+
// See https://replicate.com/docs/reference/http#create-prediction--webhook
96+
webhook: "https://your.host/webhook",
97+
98+
// See https://replicate.com/docs/reference/http#create-prediction--webhook_events_filter
99+
webhookEventsFilter: ["output", "completed"],
100+
}
101+
);
102+
```
103+
76104
## Contributing
77105

78106
While we'd love to accept contributions to this library, please open an issue

lib/Model.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export default class Model extends ReplicateObject {
122122
return prediction;
123123
}
124124

125-
async createPrediction(input) {
125+
async createPrediction(input, { webhook, webhookEventsFilter } = {}) {
126126
// This is here and not on `Prediction` because conceptually, a prediction
127127
// from a model "belongs" to the model. It's an odd feature of the API that
128128
// the prediction creation isn't an action on the model (or that it doesn't
@@ -131,6 +131,8 @@ export default class Model extends ReplicateObject {
131131
const predictionData = await this.client.request("POST /v1/predictions", {
132132
version: this.version,
133133
input,
134+
webhook,
135+
webhook_events_filter: webhookEventsFilter,
134136
});
135137

136138
return new Prediction(predictionData, this);

lib/Model.test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,44 @@ describe("createPrediction()", () => {
276276
input: { text: "test text" },
277277
});
278278
});
279+
280+
it("supports webhook URL", async () => {
281+
jest.spyOn(client, "request").mockResolvedValue({
282+
id: "testprediction",
283+
status: PredictionStatus.SUCCEEDED,
284+
});
285+
286+
await model.createPrediction(
287+
{ text: "test text" },
288+
{ webhook: "http://test.host/webhook" }
289+
);
290+
291+
expect(client.request).toHaveBeenCalledWith("POST /v1/predictions", {
292+
version: "testversion",
293+
input: { text: "test text" },
294+
webhook: "http://test.host/webhook",
295+
});
296+
});
297+
298+
it("supports webhook events filter", async () => {
299+
jest.spyOn(client, "request").mockResolvedValue({
300+
id: "testprediction",
301+
status: PredictionStatus.SUCCEEDED,
302+
});
303+
304+
await model.createPrediction(
305+
{ text: "test text" },
306+
{
307+
webhook: "http://test.host/webhook",
308+
webhookEventsFilter: ["output", "completed"],
309+
}
310+
);
311+
312+
expect(client.request).toHaveBeenCalledWith("POST /v1/predictions", {
313+
version: "testversion",
314+
input: { text: "test text" },
315+
webhook: "http://test.host/webhook",
316+
webhook_events_filter: ["output", "completed"],
317+
});
318+
});
279319
});

0 commit comments

Comments
 (0)