What You'll Learn
Building an Autonomous Ad Generation System With AI
Managing Facebook ad campaigns traditionally involves endless hours in Meta's ad manager, manually creating variations, uploading creative assets, and monitoring performance. For small teams without dedicated designers, running more than a handful of ads becomes impractical. But what if you could generate hundreds of targeted ad variations, upload them automatically, and monitor their performance—all through natural language conversation?
This tutorial walks through building exactly that: a complete AI-powered advertising automation system using Claude Code, the Gemini API, and Meta's Marketing API. The entire system runs locally on your machine, requires minimal technical knowledge, and can produce 500+ ad variations in the time it would take to manually create five.
The Three Prerequisites
Before diving into the build, you need three things:
Meta Marketing API Access: Visit developers.facebook.com, create a new app, and enable Marketing API mode in the settings. Generate an access token with full permissions—since this runs locally, there's no security concern. This token allows Claude Code to interact with your Facebook ad account.
Gemini API Key: Head to Google AI Studio and set up billing. The Gemini 3.1 Flash model ("Nano Banana 2") powers the image generation. The API costs are competitive and minimal compared to the value generated. The quality has improved dramatically—text rendering and logo reproduction are now reliable enough for production ads.
Brand Assets Collection: Gather every relevant asset: logos, event photos, product screenshots, community images, conference pictures—anything that represents your brand. These feed the AI's creative process. The more diverse your asset library, the more varied and authentic your generated ads will be.
The Creative Matrix: Forcing Diversity
Without guidance, AI tends to generate variations that look similar with minimal messaging differences. The solution is a creative matrix—a structured approach that combines personas, topics, and templates to ensure genuine diversity.
The matrix defines three dimensions:
Personas: Target audience segments like builders, founders, career switchers, or the FOMO-driven curious. Each persona has distinct pain points and motivations.
Topics: Core themes such as vibe coding, AI agents, no-code development, automation, and app building. These align with what each persona cares about most.
Templates: Visual layouts including testimonials, comparison charts, iMessage conversation mockups, community snapshots, and event highlights.
Multiplying these dimensions creates 94 unique combinations. A "founder interested in AI agents" gets both a testimonial ad and a community ad. A "career switcher exploring no-code" receives comparison and event highlight variations. This systematic approach produces ads with genuinely different messaging and visual approaches, not just cosmetic tweaks.
Claude Code helps define these parameters through conversation. You describe your product, typical customers, and preferred ad styles. It suggests personas and templates, which you refine until the matrix accurately represents your marketing strategy.
The Local Preview UI
Generating hundreds of ads is pointless if you can't review them. The system includes a simple web interface that runs locally, displaying all generated ads organized by persona and topic. You can filter by category, preview feed versus story formats, and delete unsuccessful generations.
This UI serves two critical purposes. First, it lets you validate the initial outputs before generating the full matrix. You run two or three test generations, verify the AI captured your branding correctly, then scale up. Second, it provides quality control—some generations will fail or include odd elements. The UI makes it trivial to identify and remove these outliers before upload.
Building this UI requires no coding from you. Through natural language prompts, Claude Code creates the interface, handles file organization, and sets up the preview server. You simply open a browser to localhost and start reviewing.
Meta Upload and Campaign Structure
Once you've curated your approved ads, uploading to Meta happens automatically. The system uses Meta's Marketing API to create campaigns, ad sets, and individual ads with proper targeting and budget allocation.
The integration handles the technical complexity: generating properly formatted creative payloads, managing image uploads, setting up A/B testing structures, and organizing ads into logical campaign hierarchies. You can specify budget distribution, audience targeting parameters, and testing strategies through conversation.
This automation revealed something surprising: the ads performing best weren't the ones expected. Manual campaigns tend to reflect creator bias—you make ads you think will work. When generating hundreds of variations systematically, Meta's algorithm identifies winners you would never have created. Different personas respond to different angles, and only broad testing reveals these patterns.
Performance Monitoring
The system tracks campaign performance, pulling metrics directly from Meta's API. You can ask Claude Code questions like "Which persona has the highest click-through rate?" or "Show me the top five performing ads this week." It formats the data clearly, highlighting trends and suggesting optimizations.
This conversational analytics approach makes data accessible without navigating Meta's reporting interface. You get insights when you need them, in the format you need them, without building dashboards or writing queries.
Technical Implementation Details
The entire system runs locally—no web deployment necessary. Claude Code manages the backend processes, API integrations, and file organization. The Gemini API generates images based on detailed prompts constructed from your creative matrix. Meta's Marketing API handles uploads and campaign management.
All prompts and technical setup instructions are documented for replication. You can copy the prompt sequences and adapt them to your brand, products, and target audiences. The time investment is surprisingly small: initial setup takes a few hours, generating the first batch of ads takes under an hour, and subsequent campaigns become faster as the system learns your preferences.
Cost Considerations
Running this system involves several subscription costs. Claude Code (through Anthropic or Cursor) provides the orchestration layer. The Gemini API charges per image generated—costs are minimal, typically a few cents per image. Meta ad spend is separate and depends on your campaign budget.
Compare this to hiring designers, paying for stock photos, or subscribing to design tools. For teams running regular campaigns, the ROI is clear. You're trading manual labor hours for small API fees while dramatically increasing testing volume.
Future Extensions
This foundation enables several advanced capabilities. Video ad generation using Remotion could automate motion graphics. Integration with OpenClaw could expand beyond Meta to other platforms. Custom landing pages could be generated per campaign, creating fully automated marketing funnels.
The long-term vision is autonomous marketing agents: systems that generate creative, launch campaigns, monitor performance, and optimize automatically. This project represents a practical first step toward that future.
Getting Started
Start small. Gather your assets, set up API access, and generate ten ads. Review them, refine your creative matrix, then scale up. The system improves with iteration as you learn which prompts produce the best results and which personas respond to which messages.
The Create With community provides support for builders exploring these techniques. The annual conference showcases similar projects and connects people building AI-powered tools. Whether you're automating marketing, building AI agents, or exploring vibe-coded solutions, the fundamental approach remains the same: use AI to handle repetitive creative work while you focus on strategy and optimization.





