NATIVE TYPESCRIPT RECIPES

Production TypeScript with zero SDK dependencies.

Consume API Marketplace with native standard fetch, strongly typed Pydantic-compatible response interfaces, and zero unneeded third-party package dependencies.

1// Submit an asynchronous generation request using native fetch
2interface CreateGenerationRequest {
3 character_id?: string;
4 model_alias: "photoreal-v1" | "anime-v1";
5 prompt: string;
6 aspect_ratio?: "1:1" | "2:3" | "3:2" | "3:4" | "4:3" | "9:16" | "16:9";
7 steps?: number;
8 seed?: number;
9}
10 
11interface GenerationResponse {
12 id: string;
13 status: "queued" | "processing" | "completed" | "failed";
14 createdAt: string;
15 modelAlias: string;
16}
17 
18export async function submitGeneration(
19 apiKey: string,
20 params: CreateGenerationRequest
21): Promise<GenerationResponse> {
22 const res = await fetch("https://api.apimarketplace.com/v1/generations", {
23 method: "POST",
24 headers: {
25 "Authorization": `Bearer ${apiKey}`,
26 "Content-Type": "application/json",
27 },
28 body: JSON.stringify(params),
29 });
30 
31 if (!res.ok) {
32 const error = await res.json();
33 throw new Error(`API Error: ${error.error?.message || res.statusText}`);
34 }
35 
36 return res.json();
37}
RECIPE // ASYNC POLLING

Recipe 1: Polling Generation Lifecycle

How to poll for job completion and fetch the generated asset URL.

1// Poll generation status until completed or failed
2export async function pollGeneration(
3 apiKey: string,
4 generationId: string,
5 maxAttempts = 300,
6 intervalMs = 3000
7): Promise<string> {
8 for (let attempt = 0; attempt < maxAttempts; attempt++) {
9 const res = await fetch(
10 `https://api.apimarketplace.com/v1/generations/${generationId}`,
11 {
12 headers: { "Authorization": `Bearer ${apiKey}` },
13 }
14 );
15 
16 if (!res.ok) throw new Error(await res.text());
17 const job = await res.json();
18 
19 if (job.status === "completed") {
20 // Retrieve the output asset URL
21 const assetRes = await fetch(
22 `https://api.apimarketplace.com/v1/generations/${generationId}/asset?asset_type=output`,
23 {
24 headers: { "Authorization": `Bearer ${apiKey}` },
25 }
26 );
27 if (!assetRes.ok) throw new Error(await assetRes.text());
28 const assetData = await assetRes.json();
29 return assetData.signedUrl;
30 }
31 
32 if (job.status === "failed") {
33 throw new Error(`Generation failed: ${job.errorMessage || "Unknown error"}`);
34 }
35 
36 await new Promise((resolve) => setTimeout(resolve, intervalMs));
37 }
38 
39 throw new Error("Polling timeout reached");
40}
RECIPE // WEBHOOK HANDLER

Recipe 2: Timestamped Webhook Verification

Cryptographically verify incoming X-Webhook-Signature headers in Next.js or Node.js.

1import crypto from "node:crypto";
2 
3// Pass the X-Webhook-Signature header and the original request body.
4export function verifyWebhookSignature(rawBody, signatureHeader, secret) {
5 const match = /^t=(\d+),v1=([a-f0-9]{64})$/.exec(signatureHeader || "");
6 if (!match) return false;
7 const timestamp = Number(match[1]);
8 if (Math.abs(Math.floor(Date.now() / 1000) - timestamp) > 300) return false;
9 const expected = crypto.createHmac("sha256", secret)
10 .update(`${timestamp}.`).update(rawBody).digest();
11 const actual = Buffer.from(match[2], "hex");
12 return actual.length === expected.length && crypto.timingSafeEqual(actual, expected);
13}
14// Deduplicate the verified JSON event by event.id; its type is event.event.
PRODUCTION RUNTIME

Start building in TypeScript.

Grab your API key to test these recipes in your Next.js or Node.js application.