Node.js examples

Aigeon's public API is plain JSON over HTTPS, so any HTTP client works. The snippets below use the built-in fetch (Node 18+).

Setup

Store your API key in an environment variable:

export AIGEON_API_KEY=sk_...

Create a tiny helper:

// aigeon.ts
const BASE = "https://api.aigeon.ai";

async function aigeon(path: string, init: RequestInit = {}) {
  const res = await fetch(`${BASE}${path}`, {
    ...init,
    headers: {
      "X-Api-Token": process.env.AIGEON_API_KEY!,
      "Content-Type": "application/json",
      ...(init.headers ?? {}),
    },
  });
  const body = await res.json();
  if (body.code !== 0) {
    throw new Error(`Aigeon ${path}: ${body.message}`);
  }
  return body.data;
}

Add a contact

await aigeon("/public/api/v1/contacts/add-group-members", {
  method: "POST",
  body: JSON.stringify({
    group_id: "sg_abc123",
    members: [
      { email: "alice@example.com", first_name: "Alice", tags: ["beta"] },
    ],
  }),
});

Trigger a flow

await aigeon("/public/api/v1/flow/send-email-through-flow", {
  method: "POST",
  body: JSON.stringify({
    flow_id: "flw_welcome",
    contact_email: "alice@example.com",
    context: { order_id: "ord_123", total_usd: 49.0 },
  }),
});

Pull campaign stats

const stats = await aigeon(
  "/public/api/v1/stats/get-flow-campaign-summary?flow_id=flw_weekly",
);
console.log(stats);

Handle errors properly

Wrap the helper in a retry with exponential backoff on 5xx and network failures; give up on 4xx (the request is malformed, retrying won't fix it):

async function withRetry<T>(fn: () => Promise<T>, attempts = 3): Promise<T> {
  let lastErr: unknown;
  for (let i = 0; i < attempts; i++) {
    try {
      return await fn();
    } catch (err) {
      lastErr = err;
      const msg = String(err);
      if (/4\d\d/.test(msg)) throw err; // don't retry 4xx
      await new Promise((r) => setTimeout(r, 500 * 2 ** i));
    }
  }
  throw lastErr;
}
© Aigeon.ai 2025
All Rights Reserved