This client library is designed to support the Facebook Graph API, providing a Python interface to interact with Facebook's social graph objects like users, posts, pages, and events. It also supports the official Facebook JavaScript SDK for authentication. The current stable version is 3.1.0, released in November 2018. While this project is maintained, users dealing with Meta Business (Marketing) APIs should consider the 'facebook-python-business-sdk' which is more actively developed for those specific use cases.
Install & Compatibility
Where this runs
tested against v3.1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.625s · 21.2MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 2.1s · import 0.559s · 22MB
19MB installed
● package 19MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
The primary class for interacting with the Facebook Graph API.
import facebook
graph = facebook.GraphAPI(...)
This quickstart demonstrates how to initialize the `GraphAPI` client and fetch basic user profile information. You need an access token, which can be obtained from the Facebook Developer Tools. Remember to grant appropriate permissions for the data you wish to access. Always specify the Graph API version.
import os
import facebook
# It is highly recommended to use environment variables for sensitive data like tokens
ACCESS_TOKEN = os.environ.get('FB_ACCESS_TOKEN', 'YOUR_ACCESS_TOKEN_HERE')
if not ACCESS_TOKEN or ACCESS_TOKEN == 'YOUR_ACCESS_TOKEN_HERE':
print("Please set the FB_ACCESS_TOKEN environment variable or replace 'YOUR_ACCESS_TOKEN_HERE' with your actual access token.")
else:
try:
# Initialize the Graph API with your access token
# Specify the API version for forward compatibility
graph = facebook.GraphAPI(ACCESS_TOKEN, version="2.12") # Use a specific version, e.g., "2.12" or higher
# Get information about the current user ('me')
profile = graph.get_object('me', fields='id,name,email')
print(f"Hello, {profile['name']}! Your ID is {profile['id']} and email is {profile.get('email', 'not provided')}.")
# Example: Get a list of friends (requires 'user_friends' permission)
# Note: This will only return friends who have also used the app.
# friends = graph.get_connections('me', 'friends')
# print("Your friends who use this app:")
# for friend in friends['data']:
# print(f"- {friend['name']} (ID: {friend['id']})")
except facebook.GraphAPIError as e:
print(f"Facebook Graph API Error: {e.type} - {e.message}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.