A Step-by-Step Guide to Creating a Meta Developer Account and Configuring OAuth for the Meta Marketing API (Facebook Marketing API)
Hello and welcome! Thank you for visiting my comprehensive Meta Marketing API integration setup guide. In this guide, you’ll learn how to set up a Meta Developer Account effectively, create and configure a Meta API App, and enable Marketing API permission to be able to extract marketing performance data using complete Meta API. Not to forget at the end there will be code to make your first API calls using the Python3 programming language. Whether you’re a seasoned developer or just getting started, this step-by-step guide is designed to simplify the Meta (Facebook) Marketing API Integration (configuration) process, making it easy to implement and accessible to all skill levels. Let’s get started with Meta (Facebook) API Integration!
What is the Meta Marketing API?
The Meta Marketing API (formerly known as the Facebook Marketing API) is a powerful tool used to programmatically query data, create and manage ads, and perform various marketing tasks across Meta’s platforms. This includes Facebook, Instagram, Messenger, and WhatsApp. The API is built on Meta’s Graph API and offers a wide range of endpoints for automating advertising workflows and enhancing campaign performance.
Key Use Cases of the Meta Marketing API
The Meta Marketing API is versatile and can be leveraged in various ways, including:
- Automate Ads Management
Create and manage large volumes of ad templates simultaneously, allowing you to test different creative elements, bidding strategies, or tagging configurations at scale. - Utilize Dynamic Creative
Automatically generate multiple creative variations of your ads to discover which works best for your target audience. - Build Data-Driven Audiences
Seamlessly integrate your CRM system to build custom audience segments, enhancing your ability to target the right people with personalized ads. - Optimize Campaigns in Real Time
Leverage rules-based ad management to optimize your campaigns on the go. The API allows for real-time adjustments, ensuring your ad spend is used effectively. - Create Custom Performance Dashboards
Design dashboards that pull in performance data from Meta ads and compare them with ads on other channels. This helps you better visualize and assess cross-platform ad performance.
The Meta Marketing API is a GraphQL-based API that offers flexible, programmatic interaction with Meta’s ad platforms. This guide will walk you through the necessary steps to successfully use the Meta Marketing API to manage your Facebook and other Meta platform ad campaigns.
If you want to maximize the potential of Meta Marketing API (Facebook Graph API) integration, I’m here to help. Whether you have questions, need guidance, or automated token generation solutions, or require tailored consulting services, don’t hesitate to reach out. Let’s discuss how I can assist you in achieving your goals in just no time. Here are the ways you can connect with me, and let’s start a conversation about your unique requirements.
Step 1: Create an App in Facebook Developer
To start using the Meta (Facebook) Marketing API, the first step is to create an app in the Facebook Developer Portal. Follow these simple steps to set up your app:
Access the Facebook Developer Portal
- Log in to the Facebook Developer Portal
- Navigate to the Facebook Developer Portal.
- If you’re already logged in, you’ll see the “My Apps” option. If not, click “Login” and enter your credentials.
- Go to “My Apps”
- In the top-right corner, click on “My Apps” to view your existing apps (if any) or manage your settings.
- Create a New App
Click the “Create App” button, usually located near the top-right corner of the page.
Enter App Details
- Select Business Page to connect with this page
- Select the Use case best fitted for your app
- Choose an appropriate category for your app. For this example, select “Other” to cover general marketing and advertising use cases.
- Select Your App Type
- In this guide, we’ll choose “Business” as the app type, which is most suited for the Marketing API use case. (You can select an option based on your specific use case if different).
- Fill in the App Information
- Provide essential details like the App Name and Contact Email.
- In this example, we will name the app “GuideApp”, but you can choose any relevant name based on your project.
- Create the App
Once all the information is filled out, click the “Create App” button to proceed.
Step2: Enable the Marketing API for Your App
Once you’ve completed the app creation process in Step 1, the next step is to enable the Marketing API for your app.
- Access Your App Dashboard: Once you’ve created your app, you’ll be redirected to its dashboard.
- Locate the Marketing API: Scroll down to the “Products” section and find the “Marketing API” card.
- Click “Set Up”: Click on the “Set Up” button to enable the Marketing API for your app.
Step 3: Generate Access Token and Refresh token
To interact with the Meta (Facebook) Marketing API, you’ll need to generate an Access Token and a Refresh Token. These tokens are essential for authenticating your API requests and maintaining ongoing access.
Option 1: Using a Python Script
To simplify the process, you can use a Python script to generate access tokens and refresh tokens.
To obtain the script and get assistance, please contact me via WhatsApp at +91 7892353323 or connect with me on LinkedIn.
Option 2: Using the Marketing API Tools
- Access the Tools Section: Navigate to the “Tools” section within the Marketing API.
- Select Permissions: Choose the necessary permissions, such as “ads_management,” “ads_read,” and “read_insights.”
- Generate Token: Click on the “Get Token” button.
A token will be displayed on your screen. Make sure to store it securely for future use.
Step 4: Test the Access Token
Once you have your access token, it’s important to verify its functionality.
Store your access token, refresh token, expiry time, and other credentials securely. Ideally, use cloud-based storage solutions like AWS Secrets Manager. Alternatively, store them in a .env file, which is designed to hold environment variables for a project. This method is recommended for this guide. While JSON files can be efficient for credential management, they are less secure and more vulnerable to unauthorized access.
You can also get the code to extract Meta (Facebook) Marketing account details and campaign reports from my GitHub repo through GitHub Link.
Example of a .env file:
APP_ID = "< Replace with your API app ID >" APP_SECRET = "< Replace with your API app secret >" ACCESS_TOKEN = "< Replace with your Access token >" API_VERSION = "< Replace with your API app version >"
Python3 Code for Testing the working of Access Token:
The Python script below will give you details about Facebook Business Page Details. The account id you will see in the below code output is the one you will need to use in the future to get Meta (Facebook) analytics data.
#!/usr/bin/python3 # python3 ./facebook_ads_accounts.py import sys import requests import json import datetime import os from dotenv import load_dotenv load_dotenv() def get_facebook_ads_account(access_token,api_version): try: # url = f'''https://graph.facebook.com/{api_version}/me/adaccounts? # fields=id,name,account_id,currency,timezone_id, email&access_token={access_token}''' url = f'''https://graph.facebook.com/{api_version}/me/businesses? fields=id,name,account_id,currency,timezone_id&access_token={access_token}''' headers = {} r = requests.get(url = url, headers = headers) fb_ads_account_data = r.json() #print(fb_ads_account_data) fb_ads_accounts = fb_ads_account_data['data'] for fb_ads_account in fb_ads_accounts: print("\n fb_ads_account -> ",fb_ads_account) return fb_ads_accounts except: print("\nFunction (get_facebook_ads_account) Failed",sys.exc_info()) if __name__ == '__main__': try: timestamp = datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d : %H:%M') print("DATE : ",timestamp,"\n") print("\n Process start") access_token = os.getenv("ACCESS_TOKEN") api_version = os.getenv("API_VERSION") accounts_details = get_facebook_ads_account(access_token, api_version) print("\n Account Details :\n",accounts_details) print("\n Process End \n") except: print("\n Process Failed !! ", sys.exc_info())
If you see the output that means the access_token you generated is working and you are good to retrieve meta (Facebook) marketing analytics insight using Meta Marketing API.
Congratulation! you have successfully set up Meta (Facebook) Marketing API. Now, here’s my guide to help you develop complete Python code for extracting Facebook campaign performance data using Meta API. Python script to retrieve Facebook Campaign analytics report using Meta API.
Hope I was able to solve the problem. If you like this article and think it was easy to understand do share it with your friends and connection. Thank you! see you soon.
For any suggestions or doubts ~ Get In Touch
Checkout out my other API Integration and Coding Solution Guide