Get Facebook Leads Using API
import logging
from fastapi import FastAPI
from starlette.responses import RedirectResponse
from some_get_request_handler import handle_get_endpoint
from some_post_request_handler import handle_post_endpoint, Data
from facebook_leads import get_facebook_leads
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.WARNING)
app = FastAPI()
@app.get("/", include_in_schema=False)
def root():
return RedirectResponse(url='/docs')
from facebook_leads import router as facebook_leads_router
app.include_router(facebook_leads_router)
# Do not remove the main function while updating the app.
if __name__ == "__main__":
import uvicorn
# This initialization is essential and must not be removed.
Created: | Last Updated:
Introduction to the Get Facebook Leads Using API Template
Welcome to the "Get Facebook Leads Using API" template guide. This template is designed to help you fetch Facebook lead forms for a page using the Facebook Developers API. It leverages FastAPI to create an endpoint that interacts with the Facebook API to retrieve page lead forms. Before you can use this template, you will need a Facebook access token with specific permissions. The required permissions are ads_management
, pages_read_engagement
, pages_show_list
, pages_manage_ads
, and leads_retrieval
.
This step-by-step article will guide you through the process of setting up and using this template on the Lazy platform, ensuring you can start fetching leads without worrying about deployment or environment setup.
Clicking Start with this Template
To begin using this template, click on the "Start with this Template" button. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.
Initial Setup: Adding Environment Secrets
Before testing the app, you'll need to set up the necessary environment secrets. These are not the same as environment variables in your operating system; they are secrets that can be set within the Lazy Builder under the Environment Secrets tab.
For this template, you will need to acquire a Facebook access token with the aforementioned permissions. To get this token, follow these steps:
- Go to the Facebook Developers website and navigate to your app's dashboard.
- Under the 'Settings' menu, select 'Basic' and find the 'Add Platform' button to configure your app's platform settings.
- Choose the platform you are using and provide the necessary details.
- Go to the 'Products' section and set up 'Facebook Login' if it's not already configured.
- Under 'Facebook Login', go to 'Settings' and ensure 'Client OAuth Login' and 'Web OAuth Login' are enabled.
- Use the 'Graph API Explorer' to generate an access token with the required permissions.
- Once you have the token, go back to the Lazy Builder and enter it as an environment secret.
After setting up the environment secret, you're ready to test the app.
Test: Pressing the Test Button
Press the "Test" button to begin the deployment of the app. The Lazy CLI will launch, and the app will be deployed without you needing to install libraries or set up the environment.
Entering Input: Filling in User Input
After pressing the test button, if the app requires user input, the Lazy App's CLI interface will appear, prompting you to provide the necessary input. In this case, you will be asked to enter the Facebook access token that you have set up as an environment secret.
Using the App
Once the app is running, you can interact with the API using the dedicated server link provided by the Lazy builder CLI. Additionally, since this template uses FastAPI, you will also be provided with a link to the API documentation, which can be accessed by appending /docs
to the server link. This documentation will allow you to test the API endpoints directly from your browser.
Integrating the App
If you need to integrate this app into an external service or frontend, you can use the server link provided by Lazy. For example, you can add the API endpoint to a CRM tool to automatically fetch and store leads from Facebook. Ensure that you have the correct permissions and that the external tool is configured to accept the data format provided by the API.
Here is a sample request to the API:
GET /facebook-leads?access_token=YOUR_ACCESS_TOKEN
And a sample response might look like this:
{
"data": [
{
"id": "123456789",
"name": "Lead Form",
"leads_count": 100,
// Additional lead form data
},
// More lead forms...
]
}
Replace YOUR_ACCESS_TOKEN
with the actual access token you obtained from Facebook.
By following these steps, you should be able to successfully set up and use the "Get Facebook Leads Using API" template on the Lazy platform.