code icon Code

Create Gamma

Generate a Gamma presentation, document, social post, or webpage from markdown content

Source Code

import fs from "fs";

const apiKey = "PLACEHOLDER_TOKEN";
const baseUrl = "https://public-api.gamma.app/v1.0";

const [
  content,
  format = "presentation",
  numCards = "10",
  tone = "professional",
  audience = "",
  language = "en",
] = process.argv.slice(2);

if (!content) {
  console.error("Error: content is required");
  process.exit(1);
}

async function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function startGeneration() {
  const body = {
    inputText: content,
    textMode: "preserve",
    format: format,
    numCards: parseInt(numCards, 10),
    imageOptions: {
      source: "aiGenerated",
    },
    textOptions: {
      language: language,
    },
  };

  if (tone) {
    body.textOptions.tone = tone;
  }

  if (audience) {
    body.textOptions.audience = audience;
  }

  const response = await fetch(`${baseUrl}/generations`, {
    method: "POST",
    headers: {
      "X-API-KEY": apiKey,
      "Content-Type": "application/json",
    },
    body: JSON.stringify(body),
  });

  if (!response.ok) {
    const errorText = await response.text();
    throw new Error(`API error (${response.status}): ${errorText}`);
  }

  return response.json();
}

async function pollStatus(generationId) {
  const response = await fetch(`${baseUrl}/generations/${generationId}`, {
    headers: {
      "X-API-KEY": apiKey,
      Accept: "application/json",
    },
  });

  if (!response.ok) {
    const errorText = await response.text();
    throw new Error(`Poll error (${response.status}): ${errorText}`);
  }

  return response.json();
}

async function main() {
  console.log(`Starting Gamma generation...`);
  console.log(`Format: ${format}`);
  console.log(`Cards: ${numCards}`);
  console.log(`Content length: ${content.length} characters`);

  const startResult = await startGeneration();
  const generationId = startResult.generationId;

  console.log(`Generation started: ${generationId}`);
  console.log(`Polling for completion...`);

  let attempts = 0;
  const maxAttempts = 60;

  while (attempts < maxAttempts) {
    await sleep(3000);
    attempts++;

    const status = await pollStatus(generationId);

    if (status.status === "completed") {
      console.log(`✓ Generation complete!`);
      console.log(
        JSON.stringify(
          {
            success: true,
            generationId: generationId,
            gammaUrl: status.gammaUrl,
            format: format,
            numCards: parseInt(numCards, 10),
            credits: status.credits,
          },
          null,
          2
        )
      );
      return;
    }

    if (status.status === "failed" || status.error) {
      throw new Error(`Generation failed: ${status.error || "Unknown error"}`);
    }

    console.log(
      `Status: ${status.status} (attempt ${attempts}/${maxAttempts})`
    );
  }

  throw new Error("Generation timed out after 3 minutes");
}

main().catch((err) => {
  console.error("Error:", err.message);
  process.exit(1);
});