io.backgroundFetch()
io.backgroundFetch()
allows you to fetch data from a URL that can take longer that the serverless timeout. The actual fetch
request is performed on the Trigger.dev platform, and the response is sent back to you. An example use case is fetching data from a slow API, like some AI endpoints.
This is used inside the OpenAI Integration for Tasks like Chat Completions Background Create
Parameters
Should be a stable and unique cache key inside the run()
. See
resumability for more information.
The url to fetch.
Options for the request
The HTTP method to use for the request.
Any headers to send with the request. Note that you can use redactString to prevent sensitive information from being stored (e.g. in the logs), like API keys and tokens.
The body of the request.
An object where the key is a status code pattern and the value is a retrying strategy. Supported patterns are:
- Specific status codes: 429
- Ranges: 500-599
- Wildcards: 2xx, 3xx, 4xx, 5xx
{
"500-599": {
strategy: "backoff",
limit: 5,
minTimeoutInMs: 1000,
maxTimeoutInMs: 30000,
factor: 1.8,
randomize: true,
},
"429": {
strategy: "backoff",
limit: 10,
minTimeoutInMs: 1000,
maxTimeoutInMs: 60000,
factor: 2,
randomize: true,
},
}
An individual retrying strategy can be one of two types:
backoff
: Retries the request with an exponential backoff.headers
: Retries the request using info from the response headers.
Allows you to set timeouts for the request, as well as specific retry strategies for timeouts.
The amount of time to wait before timing out the request. The minimum value is 1s and max is 2min
Returns
A Promise
that resolves with the JSON response body of the background fetch request. You can specify the type of the response body as a generic parameter.
Was this page helpful?