Documentation Cloudflare Worker SDK
5 min read | June 29, 2026
Tutorial

Cloudflare Worker SDK

The Astralog Cloudflare Worker SDK transparently monitors all your interactions on the Edge. It captures request latency, errors, and custom events using ctx.waitUntil() for zero-blocking performance.

View on GitHub

Installation

npm install @astraloghq/cloudflare

Setup

import { Astralog } from '@astraloghq/cloudflare';

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
    // 1. Initialize Astralog for this request
    const client = new Astralog({ apiKey: env.ASTRALOG_API_KEY }, ctx);
    
    // 2. Log events instantly (does not block CPU)
    client.info("Incoming request", { url: request.url, method: request.method });

    try {
      // Your business logic
      const response = new Response("Hello from the Edge!");
      
      // 3. Graceful shutdown (Waits for pending logs in background)
      client.flush();
      
      return response;
      
    } catch (error) {
      client.error("Request failed", { error: error.message });
      client.flush();
      return new Response("Internal Error", { status: 500 });
    }
  }
};

Cost Control & Visibility

By integrating Astralog into your Edge computing environment, we generate specific edge_inference events in ClickHouse. You can use our Query Interface to instantly calculate your bandwidth usage, latency distributions, and detect anomalies at the Edge.