Skip to content

Commit 3e28ba9

Browse files
committed
Support webhooks when creating predictions
1 parent 8394541 commit 3e28ba9

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
@@ -16,6 +16,8 @@ Install with `npm install replicate`
1616

1717
Set your API token as an environment variable called `REPLICATE_API_TOKEN`.
1818

19+
### Making preedictions
20+
1921
To run a prediction and return its output:
2022

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

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

80108
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)