pause_campaigns_batch
Pause multiple Meta ad campaigns in bulk using a single Batch API call. Supports up to 50 campaign IDs per request; larger arrays are automatically chunked with a 2-second delay to avoid rate limits.
Instructions
WRITE (BULK): Pause many campaigns in a single Meta Batch API call (up to 50/request; arrays bigger than 50 are chunked automatically with a 2s delay between chunks to sidestep rate-limit code 17). Returns {results: Array<{code, body}>} — one entry per campaign. code: 200 = success.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| campaign_ids | Yes | Meta Campaign IDs to pause |
Implementation Reference
- src/tools/bulk.ts:69-79 (handler)Handler function that pauses campaigns via Meta Batch API. It deduplicates campaign IDs, builds POST requests with status=PAUSED, sends them via metaBatch(), and returns the count + results.
handler: async (args) => { const ids = Array.from(new Set(args.campaign_ids as string[])); const requests: MetaBatchRequest[] = ids.map((id) => ({ method: "POST", relative_url: id, body: encodeBatchBody({ status: "PAUSED" }), })); const results = await metaBatch(requests); return { requested: ids.length, results }; }, }, - src/tools/bulk.ts:63-68 (schema)Input schema for pause_campaigns_batch: requires a non-empty array of campaign ID strings.
inputSchema: { campaign_ids: z .array(z.string()) .min(1) .describe("Meta Campaign IDs to pause"), }, - src/tools/bulk.ts:56-57 (registration)Tool definition entry with name 'pause_campaigns_batch' exported in the bulkTools array, which is imported and registered in both src/index.ts (line 55) and src/http.ts (line 38).
{ name: "pause_campaigns_batch", - src/tools/bulk.ts:12-23 (helper)Helper function that encodes the {status: 'PAUSED'} body into URL-encoded format for Meta's Batch API.
function encodeBatchBody(obj: Record<string, unknown>): string { const p = new URLSearchParams(); for (const [k, v] of Object.entries(obj)) { if (v === undefined || v === null) continue; if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") { p.append(k, String(v)); } else { p.append(k, JSON.stringify(v)); } } return p.toString(); } - src/client.ts:234-263 (helper)The metaBatch() function that executes batch requests in chunks of 50 with a 2s delay between chunks (rate-limit sidestep). This is the actual API caller used by the handler.
export async function metaBatch( requests: MetaBatchRequest[], ): Promise<MetaBatchResponse[]> { const CHUNK = 50; const results: MetaBatchResponse[] = []; for (let i = 0; i < requests.length; i += CHUNK) { const slice = requests.slice(i, i + CHUNK); const form = new URLSearchParams(); form.append("access_token", getCurrentToken()); form.append("batch", JSON.stringify(slice)); const res = await fetch(META_API_BASE + "/", { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: form.toString(), }); if (!res.ok) { const text = await res.text().catch(() => ""); throw new Error(enhanceMetaError(res.status, text)); } const chunkResults = (await res.json()) as MetaBatchResponse[]; results.push(...chunkResults); // Space chunks out a bit to stay under per-account call-volume thresholds. if (i + CHUNK < requests.length) { await new Promise((r) => setTimeout(r, 2000)); } } return results; }