client.defineJob({
  id: "background-fetch-job",
  name: "Background fetch Job",
  version: "0.0.1",
  trigger: eventTrigger({
    name: "example.event",
  }),
  run: async (payload, io, ctx) => {
    const response = io.backgroundFetch<CreateChatCompetionResponseData>(
      "fetch-some-data",
      "https://example.com/api/endpoint",
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: redactString`Bearer ${auth.apiKey}`,
        },
        body: JSON.stringify({ foo: "bar" }),
      },
      {
        "429": {
          strategy: "headers",
          limitHeader: "x-ratelimit-limit",
          remainingHeader: "x-ratelimit-remaining",
          resetHeader: "x-ratelimit-reset",
        },
        "5xx": {
          strategy: "backoff",
          limit: 5,
          minTimeoutInMs: 1000,
          maxTimeoutInMs: 30000,
          factor: 1.8,
          randomize: true,
        },
      }
    );

    return response;
  },
});

These are the docs for Trigger.dev v2 which will be deprecated on January 31st, 2025. You probably want the v3 docs.

This is used inside the OpenAI Integration for Tasks like Chat Completions Background Create

Parameters

cacheKey
string
required

Should be a stable and unique cache key inside the run(). See resumability for more information.

url
string
required

The url to fetch.

options
object

Options for the request

retry options
object

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.
timeout options
object

Allows you to set timeouts for the request, as well as specific retry strategies for timeouts.

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.

client.defineJob({
  id: "background-fetch-job",
  name: "Background fetch Job",
  version: "0.0.1",
  trigger: eventTrigger({
    name: "example.event",
  }),
  run: async (payload, io, ctx) => {
    const response = io.backgroundFetch<CreateChatCompetionResponseData>(
      "fetch-some-data",
      "https://example.com/api/endpoint",
      {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: redactString`Bearer ${auth.apiKey}`,
        },
        body: JSON.stringify({ foo: "bar" }),
      },
      {
        "429": {
          strategy: "headers",
          limitHeader: "x-ratelimit-limit",
          remainingHeader: "x-ratelimit-remaining",
          resetHeader: "x-ratelimit-reset",
        },
        "5xx": {
          strategy: "backoff",
          limit: 5,
          minTimeoutInMs: 1000,
          maxTimeoutInMs: 30000,
          factor: 1.8,
          randomize: true,
        },
      }
    );

    return response;
  },
});