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,
  fetchConfigOrRetryConfig?: FetchConfig | ContextFunctionRetryConfig,
  retryConfig?: ContextFunctionRetryConfig
): Promise<T | undefined>
Where FetchConfig is:
export interface FetchConfig {
  method?: string;
  headers?: Record<string, string>;
  body?: Record<string, unknown> | string;
}
And ContextFunctionRetryConfig is:
type ContextFunctionRetryConfig = {
  max_attempts: number;
  initial_interval_ms: number;
  backoff_factor: number;
};

Examples

Basic GET Request

import { TaskContext } from "compose";

export async function main({ fetch }: TaskContext) {
  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
import { TaskContext } from "compose";

export async function main({ fetch }: TaskContext) {
  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

import { TaskContext } from "compose";

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

  return data;
}

Next Steps