Fetch: performing API requests or fetching data TypeScript only
The Inngest TypeScript SDK provides a step.fetch()
API and a fetch()
utility, enabling you to make requests to third-party APIs or fetch data in a durable way by offloading them to the Inngest Platform.
For more information on how Fetch works, see the Fetch documentation.
Getting started with step.fetch()
The step.fetch()
API enables you to make durable HTTP requests while offloading them to the Inngest Platform, saving you compute and improving reliability:
src/inngest/functions.ts
import { inngest } from "./client";
export const retrieveTextFile = inngest.createFunction(
{ id: "retrieveTextFile" },
{ event: "textFile/retrieve" },
async ({ step }) => {
// The fetching of the text file is offloaded to the Inngest Platform
const response = await step.fetch(
"https://example-files.online-convert.com/document/txt/example.txt"
);
// The Inngest function run is resumed when the HTTP request is complete
await step.run("extract-text", async () => {
const text = await response.text();
const exampleOccurences = text.match(/example/g);
return exampleOccurences?.length;
});
}
);
step.fetch()
takes the same arguments as the native fetch
API.
Parallelize HTTP requests with step.fetch()
step.fetch()
shares all the benefits of step.run()
, including the ability to parallelize requests using Promise.all()
:
const processFiles = inngest.createFunction(
{ id: "process-files", concurrency: 10 },
{ event: "files/process" },
async ({ step, event }) => {
// All requests will be offloaded and processed in parallel while matching the concurrency limit
const responses = await Promise.all(event.data.files.map(async (file) => {
return step.fetch(`https://api.example.com/files/${file.id}`)
}))
// Your Inngest function is resumed here with the responses
await step.run("process-file", async (file) => {
const body = await response.json()
// body.files
})
}
)
Note that step.fetch()
, like all other step
APIs, matches your function's configuration such as concurrency or throttling.
Make 3rd party library HTTP requests durable with the fetch()
utility
Inngest's fetch()
utility can be passed as a custom fetch handler to make all the requests made by a 3rd party library durable.
For example, you can pass the fetch()
utility to the OpenAI node client as follows:
import { fetch } from "inngest";
import OpenAI from 'openai';
const client = new OpenAI({ fetch });
// use the global fetch
const completion = await client.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: "Hello, world!" }],
});
const weatherFunction = inngest.createFunction(
{ id: "weather-function" },
{ event: "weather/get" },
async ({ step }) => {
// The OpenAI request is automatically offloaded to the Inngest Platform
const completion = await client.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [{ role: "user", content: "What's the weather in London?" }],
});
}
)