Skip to main content

Auditable HTTP Requests with “fetch”

Make HTTP requests with automatic retries and logging. This is how compose apps interact with outside, off-chain systems. All fetch behavior is fully recorded in our event logs, making Compose App functionality fully auditable. For example, if you have an oracle that is advertised to be driven by an aggregate of several well known price feeds, users of DApps built with this oracle can audit the calls made to determine their prices, ensuring the price logic is fair and works as advertised.
fetch<T = unknown>(
  url: string,
  options?: {
    method?: string;
    headers?: Record<string, string>;
    body?: Record<string, unknown> | string;
  },
  retryConfig?: HostFunctionRetryConfig
): Promise<T>

Examples

Basic GET Request

/// <reference types="../../.compose/types.d.ts" />

export async function main({ fetch }) {
  const data = await fetch("https://api.example.com/data");
  return data;
}

POST Request with JSON Body

POST requests can be used to trigger other tasks from within a task
/// <reference types="../../.compose/types.d.ts" />

export async function main({ fetch }) {
  const result = await fetch("https://api.example.com/submit", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: "Bearer token123",
    },
    body: {
      name: "John Doe",
      email: "john@example.com",
    },
  });

  return result;
}

Request with Custom Retry Configuration

/// <reference types="../../.compose/types.d.ts" />

export async function main({ fetch }) {
  const data = await fetch(
    "https://unreliable-api.com/data",
    {
      method: "GET",
      headers: { "User-Agent": "Compose/1.0" },
    },
    {
      maxAttempts: 5,
      initialIntervalMs: 2000,
      backoffFactor: 1.5,
    }
  );

  return data;
}

Next Steps