Use Case

Automate Facebook Ad Videos with the API

Build Facebook ad pipelines that generate UGC videos programmatically. Use the agent-media TypeScript SDK to create 1:1 and 16:9 creatives, batch A/B variants across actors and scripts, and push finished videos directly to Meta Ads Manager via their API. See pricing.

The Problem: Manual Video Production Doesn't Scale

Facebook ad accounts burn through creatives fast. Audiences fatigue on the same video within days, so you need a constant pipeline of fresh variants. Most teams solve this by hiring creators or using browser-based tools that require manual clicks for every video.

If you are building ad infrastructure, you need an API. You need to generate videos from code, trigger new creatives when performance drops, and push them into Meta Ads Manager without a human in the loop. Browser tools and manual workflows do not fit into a CI/CD pipeline or a cron job.

The result: engineering teams either build fragile video pipelines from scratch or accept a manual bottleneck in an otherwise automated ad system.

Generate a Facebook Ad with the SDK

Install the SDK with npm i agent-media and generate a Facebook-optimized video in a few lines.

generate-facebook-ad.ts
import AgentMedia from "agent-media";

const client = new AgentMedia({
  apiKey: process.env.AGENT_MEDIA_API_KEY,
});

// Generate a 1:1 square video for Facebook Feed
const video = await client.videos.create({
  script: "I tried every budgeting app. This one actually stuck.",
  actor: "sofia",
  aspectRatio: "1:1",   // Facebook Feed / Instagram
  subtitleStyle: "hormozi",
  duration: 10,
});

// video.url → https://media.agent-media.ai/xxx.mp4
console.log(video.url);

Use aspectRatio: "16:9" for Facebook In-Stream and Reels, or "9:16" for Stories. The API returns a hosted mp4 URL you can pass directly to Meta.

Batch Generation: A/B Test Actors and Scripts

Generate multiple variants in parallel. Test different actors with the same script, or different scripts with the same actor.

batch-variants.ts
import AgentMedia from "agent-media";

const client = new AgentMedia({
  apiKey: process.env.AGENT_MEDIA_API_KEY,
});

const script = "I tried every budgeting app. This one actually stuck.";
const actors = ["sofia", "emma", "naomi", "marco", "priya"];

// Generate 5 A/B variants in parallel
const videos = await Promise.all(
  actors.map((actor) =>
    client.videos.create({
      script,
      actor,
      aspectRatio: "1:1",
      subtitleStyle: "hormozi",
      duration: 10,
    })
  )
);

// Each video has a unique actor, same script
for (const video of videos) {
  console.log(`${video.actor}: ${video.url}`);
}

Push Videos to Meta Ads Manager

The API returns a hosted video URL. Upload it to your Meta Ad Account using the Marketing API, then create ad creatives from it.

upload-to-meta.ts
import AgentMedia from "agent-media";

const client = new AgentMedia({
  apiKey: process.env.AGENT_MEDIA_API_KEY,
});

// 1. Generate the video
const video = await client.videos.create({
  script: "Stop scrolling. This changed how I save money.",
  actor: "sofia",
  aspectRatio: "1:1",
  subtitleStyle: "bold",
  duration: 10,
});

// 2. Upload to Meta via Marketing API
const adAccountId = process.env.META_AD_ACCOUNT_ID;
const metaToken = process.env.META_ACCESS_TOKEN;

const res = await fetch(
  `https://graph.facebook.com/v21.0/${adAccountId}/advideos`,
  {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      access_token: metaToken,
      file_url: video.url,   // hosted mp4 from agent-media
      title: `UGC Ad — ${video.actor}`,
    }),
  }
);

const { id: videoId } = await res.json();
console.log("Meta video ID:", videoId);

From here, use the Meta Marketing API to create an adcreative referencing the video ID and attach it to your campaign. The full pipeline runs without any browser interaction.

CLI: Quick Ad Creation

Use the CLI for one-off generation or shell scripts. The --sync flag waits for the video to finish and prints the URL.

terminal

# Generate a 1:1 Facebook Feed ad

$ agent-media ugc "Stop scrolling. This changed how I save money." \

--actor sofia --style hormozi --aspect-ratio 1:1 --duration 10 --sync

# Generate 5 variants in a loop

$ for actor in sofia emma naomi marco priya; do \

agent-media ugc "Stop scrolling..." --actor $actor --style hormozi --sync; \

done

Webhook: Async Processing

For production pipelines, use webhooks instead of polling. Set a webhookUrl and agent-media will POST the completed video to your endpoint.

webhook-example.ts
// Fire-and-forget: agent-media POSTs to your endpoint when done
const video = await client.videos.create({
  script: "This app saved me $400 last month.",
  actor: "emma",
  aspectRatio: "1:1",
  subtitleStyle: "hormozi",
  duration: 10,
  webhookUrl: "https://your-api.com/hooks/video-ready",
});

// video.id is returned immediately
// Your webhook receives the full payload when rendering completes:
// {
//   "id": "vid_abc123",
//   "status": "completed",
//   "url": "https://media.agent-media.ai/xxx.mp4",
//   "actor": "emma",
//   "duration": 10,
//   "aspectRatio": "1:1"
// }

Cost Breakdown

ScenarioCost
1 ad creative (10s)~$3 (300 credits)
5 A/B variants (same script)~$15 (1,500 credits)
10 creatives per week~$30 (3,000 credits)
Creator plan ($39/mo)13 creatives per month
Pro Plus plan ($129/mo)43 creatives per month

Five A/B variants for $15 via API, versus $750+ with freelance creators. View full pricing.

Why agent-media for Facebook Ad Pipelines

TypeScript SDK + REST API

First-class TypeScript SDK with full type safety. Or hit the REST API directly from any language. No browser automation required.

200+ actors, all via API

Select actors by ID. Diverse demographics and styles. Generate variants programmatically without browsing a catalog.

Webhook delivery

Fire-and-forget video generation. Your endpoint receives the finished video URL when rendering completes. No polling needed.

Meta Ads API compatible

Returns hosted mp4 URLs that work directly with the Meta Marketing API file_url parameter. Zero download/re-upload steps.