Learn How to get Meta Ads Account Details using Python and Meta Access Token
Welcome to this comprehensive guide! This article demonstrates how to develop Python code to extract key details from your Meta (Facebook) Ads Account, such as Account ID, Name, Currency, and more. Utilizing the Facebook Marketing API, we’ll guide you through the process of retrieving this valuable information. Understanding your Account ID is crucial for subsequent tasks, such as extracting campaign data using the Meta (Facebook) API.
Feel free to adapt this code and the concepts presented here to meet your data extraction needs.
There are two main ways to retrieve Facebook Ads Account details, particularly the Account ID:
- Directly from Facebook Ads Manager: You can find your Account ID within the Facebook Ads Manager interface.
- Using Python code and the Facebook Marketing API: This method allows you to programmatically extract your Account ID and other crucial details.

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. The Meta Marketing API is a GraphQL-based API that offers flexible, programmatic interaction with Meta’s ad platforms.
Quick Access :
- Meta (Facebook) Marketing API Integration Guide
- Guide to building Python code to retrieve Facebook Campaign (Advertising) Insight using Meta API
- Guide to get Meta (Facebook) Ads Account details (You are Here)
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.
Method 1: Getting Ads Account ID from Facebook Campaign Manager
- Login to Facebook Ads Manager: Begin by logging into your Facebook Ads Manager account.
- Locate Account List: Upon successful login, you’ll be presented with a list of Facebook Ads Accounts you have access to. This may include your own account, or accounts that have been shared with you.
- Find Account ID: * From Account Selection Dropdown: The Account ID can be found within the dropdown menu where you select the account for your campaigns. * From URL: The Account ID is also typically included in the URL of the Facebook Ads Manager pages.

Method 2: Python code to get Meta (Facebook) Ad Account Details
In this method, we will build python logic to get Facebook Ads Account ID and other details (like location, currency, etc). Python code will be using the Facebook API. Hence it’s important you have completed the Facebook API setup steps and you have all the tokens to create the below credential file.
2.1- Securely Store Tokens
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 .envfile, 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.
Let’s create a .env file to manage your Meta API credentials.
ACCESS_TOKEN = "< Replace with your Access token >" API_VERSION = "< Replace with your API app version >" APP_ID = "< Replace with your API app ID >" APP_SECRET = "< Replace with your API app secret >"
You can also get the code to extract Meta (Facebook) Marketing account details and campaign reports from my GitHub repo through GitHub Link.
2.2- Building Python Logic to get Account ID
Here, we will first read the credentials from the above JSON file (line number: 46 – 49). Next, we are going to define a function (line number: 9 – 37) to make the Facebook API call and get Ads Account details in the response. The last one calls the above function (line number: 52).
Consider going through the code, and try to get a basic understanding of what’s going on. Don’t forget to save the code file as “facebook_ads_accounts.py”.
#!/usr/bin/python3 import sys import requests import json def get_facebook_ads_account(access_token,api_version): try: url = "https://graph.facebook.com/"+api_version+"/me/adaccounts?fields=name,account_id,currency,timezone_id&access_token="+access_token headers = {} r = requests.get(url = url, headers = headers) fb_ads_account_data = r.json() fb_ads_accounts = fb_ads_account_data['data'] for fb_ads_account in fb_ads_accounts: print("\nfb_ads_account -> ",fb_ads_account) except: print("\nFunction (get_facebook_ads_account) Failed",sys.exc_info()) if __name__ == '__main__': try: print("Facebook Ads Account extraction process Started") #reading client_id json file cred_file = "./facebook_cred.json" facebook_cred = open(cred_file, 'r') cred_json = json.load(facebook_cred) access_token = cred_json["access_token"] app_id = cred_json["app_id"] app_secret = cred_json["app_secret"] api_version = cred_json["api_version"] accounts_details_df = get_facebook_ads_account(access_token,api_version) except: print("\nFacebook Ads Account extraction process Failed",sys.exc_info())
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.
If you have any questions or comments feel free to reach me at ->
Checkout out my other API Integration and Coding Solution Guide