hcassar-meta-ads
v0.0.1
Published
CLI for Meta (Facebook) Ads API with Node.js. Access campaigns, ad sets, ads, and insights.
Downloads
92
Maintainers
Readme
Meta Ads CLI
A command-line interface for the Meta (Facebook) Ads API, enabling programmatic access to campaigns, ad sets, ads, and insights. Built with Node.js and TypeScript.
Features
- 🔐 OAuth 2.0 Authentication - Secure authentication with long-lived tokens
- 📊 Campaign Management - List and view campaigns, ad sets, and ads
- 📈 Insights & Reporting - Access performance metrics and campaign insights
- 👥 Multi-Profile Support - Manage multiple Meta Ads accounts
- 🎨 Rich Output - Table formatting and JSON export options
- 💾 Secure Storage - Encrypted credential storage with 0600 file permissions
- 🔄 Multiple Ad Accounts - Support for managing multiple ad accounts per profile
Prerequisites
Before using this CLI, you need:
- Meta Developer Account with an app created
- App ID and App Secret from your Meta app
- Ad Account Access - You must have access to at least one Meta Ads account
Setup Guide
1. Create a Meta App
- Go to Meta for Developers
- Click My Apps > Create App
- Choose Business as the app type
- Fill in the app details and create the app
2. Configure Your App
- In your app dashboard, go to Settings > Basic
- Copy your App ID and App Secret
- Add Product: Select Facebook Login
- Under Facebook Login > Settings:
- Add Valid OAuth Redirect URIs:
http://localhost:3000/oauth/callback - Save changes
- Add Valid OAuth Redirect URIs:
3. Get Access to Marketing API
- In your app, go to Add Products
- Click Set Up on Marketing API
- Request Standard Access if needed (for production use)
4. Get Your Ad Account ID
- Go to Meta Ads Manager
- Your Ad Account ID is visible in the URL:
act_XXXXXXXXXX - Copy this ID (including the
act_prefix)
Installation
Option 1: Install from npm (Recommended)
npm install -g meta-ads-cliOption 2: Install from Source
git clone https://github.com/hcassar93/meta-ads-cli.git
cd meta-ads-cli
npm install
npm run build
npm link # Install globallyVerify Installation
meta-ads-cli --version
meta-ads-cli --helpQuick Start
1. Initial Setup
Configure your credentials:
meta-ads-cli setupYou'll be prompted for:
- Profile name (e.g., "default", "client1", etc.)
- Meta App ID
- Meta App Secret
- Ad Account ID (optional, format: act_XXXXXXXXXX)
2. Authenticate
Run the OAuth flow to get access tokens:
meta-ads-cli authThis will:
- Open your browser for Meta authentication
- Prompt you to authorize the application
- Store long-lived access tokens securely (~60 days)
3. Start Using Commands
List your ad accounts:
meta-ads-cli accountsList campaigns:
meta-ads-cli campaigns -a act_123456789Usage
Authentication Commands
Setup
meta-ads-cli setupConfigure API credentials for a new profile.
Authenticate
meta-ads-cli auth [-p <profile>]Authenticate and obtain access tokens.
Logout
meta-ads-cli logout [-p <profile>]Clear stored credentials.
View Configuration
meta-ads-cli config [-p <profile>]Display current configuration.
Manage Profiles
# List all profiles
meta-ads-cli profiles --list
# Switch active profile
meta-ads-cli profiles --switchAccount Commands
List Ad Accounts
meta-ads-cli accounts [--json]Shows all ad accounts you have access to.
Get Account Details
meta-ads-cli account <account-id> [--json]Example:
meta-ads-cli account act_123456789Campaign Commands
List Campaigns
meta-ads-cli campaigns [-a <account-id>] [-l <limit>] [--json]Options:
-a, --account-id <id>- Ad Account ID (required if not set in profile)-l, --limit <number>- Maximum campaigns to return (default: 50)--json- Output as JSON
Example:
meta-ads-cli campaigns -a act_123456789 -l 10View Campaign Details
meta-ads-cli campaign <campaign-id> [--insights] [--json]Options:
--insights- Include performance insights (last 30 days)
Example:
meta-ads-cli campaign 120213377777777 --insightsList Ad Sets
meta-ads-cli adsets <campaign-id> [-l <limit>] [--json]Example:
meta-ads-cli adsets 120213377777777Configuration
Configuration Location
Credentials are stored in:
~/.meta-ads-cli/config.jsonFile permissions are automatically set to 0600 (owner read/write only).
Multi-Profile Support
Manage multiple Meta Ads accounts:
# Create additional profiles during setup
meta-ads-cli setup
# Enter a unique profile name when prompted
# List profiles
meta-ads-cli profiles --list
# Switch active profile
meta-ads-cli profiles --switch
# Use specific profile for a command
meta-ads-cli campaigns -p my-other-account -a act_123456789Output Formats
Table Format (Default)
meta-ads-cli campaigns -a act_123456789
┌──────────────────┬────────────────────────────┬────────┬──────────────┬───────────────┬────────────┐
│ ID │ Name │ Status │ Objective │ Daily Budget │ Start Time │
├──────────────────┼────────────────────────────┼────────┼──────────────┼───────────────┼────────────┤
│ 120213377777777 │ Summer Sale Campaign │ ACTIVE │ CONVERSIONS │ $50.00 │ 1/15/2024 │
└──────────────────┴────────────────────────────┴────────┴──────────────┴───────────────┴────────────┘JSON Format
meta-ads-cli campaigns -a act_123456789 --json[
{
"id": "120213377777777",
"name": "Summer Sale Campaign",
"status": "ACTIVE",
"objective": "CONVERSIONS",
"daily_budget": "5000",
"start_time": "2024-01-15T08:00:00+0000"
}
]Troubleshooting
"Not authenticated" Error
Run the auth command:
meta-ads-cli auth"No profile found" Error
Create a profile first:
meta-ads-cli setup"Ad Account ID required" Error
Either:
- Provide
--account-idflag:meta-ads-cli campaigns -a act_123456789 - Or set it during setup:
meta-ads-cli setupand enter your Ad Account ID
Token Expired Error
Tokens are long-lived (~60 days) but do expire. Re-authenticate:
meta-ads-cli authPort 3000 Already in Use
The OAuth callback uses port 3000. Close conflicting applications or modify src/auth/oauth.ts to use a different port.
"Invalid OAuth access token" Error
Your access token may be invalid or expired. Try:
- Re-authenticating:
meta-ads-cli auth - Creating a new profile:
meta-ads-cli setup
API Limits
- Marketing API: Rate-limited per app and user
- Test Mode: Limited to accounts you own or manage
- Standard Access: Required for production use with broader access
Development
Build
npm run buildDevelopment Mode
npm run dev -- <command>
# Examples
npm run dev -- setup
npm run dev -- campaigns -a act_123456789
npm run dev -- campaign 120213377777777 --insightsProject Structure
meta-ads-cli/
├── src/
│ ├── index.ts # CLI entry point
│ ├── cli.ts # Commander setup
│ ├── auth/ # Authentication logic
│ │ ├── oauth.ts
│ │ └── setup.ts
│ ├── api/
│ │ └── meta-ads.ts # Meta Ads API wrapper
│ ├── commands/ # CLI command implementations
│ │ ├── accounts.ts
│ │ └── campaigns.ts
│ └── utils/ # Utilities
│ └── config.ts
├── dist/ # Compiled JavaScript
├── package.json
└── tsconfig.jsonResources
- Meta Marketing API Documentation
- Meta Graph API Explorer
- OAuth Documentation
- Meta Business SDK for Node.js
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
Author
Hayden Cassar
Note: This tool requires proper Meta app setup and API access. It is designed for developers and marketers with technical knowledge of APIs.
