Skip to content

Commit 20983bc

Browse files
authoredMar 13, 2024··
Document webhooks in README
1 parent ba9b94e commit 20983bc

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed
 

‎README.md

+60-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ console.log(prediction.output);
7373
// ['https://replicate.delivery/pbxt/RoaxeXqhL0xaYyLm6w3bpGwF5RaNBjADukfFnMbhOyeoWBdhA/out-0.png']
7474
```
7575

76-
To run a model that takes a file input, pass a URL to a publicly accessible file. Or, for smaller files (<10MB), you can pass the data directly.
76+
To run a model that takes a file input you can pass the data directly or pass a URL to a publicly accessible file.
7777

7878
```js
7979
const fs = require("node:fs/promises");
@@ -89,6 +89,65 @@ const output = await replicate.run(model, { input });
8989
// ['https://replicate.delivery/mgxm/e7b0e122-9daa-410e-8cde-006c7308ff4d/output.png']
9090
```
9191

92+
### Webhooks
93+
94+
Webhooks provide real-time updates about your prediction. Specify an endpoint when you create a prediction, and Replicate will send HTTP POST requests to that URL when the prediction is created, updated, and finished.
95+
96+
It is possible to provide a URL to the predictions.create() function that will be requested by Replicate when the prediction status changes. This is an alternative to polling.
97+
98+
To receive webhooks you’ll need a web server. The following example uses Hono, a web standards based server, but this pattern applies to most frameworks.
99+
100+
<details>
101+
<summary>See example</summary>
102+
103+
```js
104+
import { serve } from '@hono/node-server';
105+
import { Hono } from 'hono';
106+
107+
const app = new Hono();
108+
app.get('/webhooks/replicate', async (c) => {
109+
// Get the prediction from the request.
110+
const prediction = await c.req.json();
111+
console.log(prediction);
112+
//=> {"id": "xyz", "status": "successful", ... }
113+
114+
// Acknowledge the webhook.
115+
c.status(200);
116+
c.json({ok: true});
117+
}));
118+
119+
serve(app, (info) => {
120+
console.log(`Listening on http://localhost:${info.port}`)
121+
//=> Listening on http://localhost:3000
122+
});
123+
```
124+
125+
</details>
126+
127+
Create the prediction passing in the webhook URL to `webhook` and specify which events you want to receive in `webhook_events_filter` out of "start", "output", ”logs” and "completed":
128+
129+
```js
130+
const Replicate = require("replicate");
131+
const replicate = new Replicate();
132+
133+
const input = {
134+
image: "https://replicate.delivery/pbxt/KWDkejqLfER3jrroDTUsSvBWFaHtapPxfg4xxZIqYmfh3zXm/Screenshot%202024-02-28%20at%2022.14.00.png",
135+
denoising_strength: 0.5,
136+
instant_id_strength: 0.8
137+
};
138+
139+
const callbackURL = `https://my.app/webhooks/replicate`;
140+
await replicate.predictions.create({
141+
version: "19deaef633fd44776c82edf39fd60e95a7250b8ececf11a725229dc75a81f9ca",
142+
input: input,
143+
webhook: callbackURL,
144+
webhook_events_filter: ["completed"],
145+
});
146+
147+
// The server will now handle the event and log:
148+
// => {"id": "xyz", "status": "successful", ... }
149+
```
150+
92151
## TypeScript
93152

94153
Currently in order to support the module format used by `replicate` you'll need to set `esModuleInterop` to `true` in your tsconfig.json.

0 commit comments

Comments
 (0)
Please sign in to comment.