- pub
How to Use FastAPI to Wrap Luma AI's API: A Practical Guide
1. Introduction to Lumalabs and Dream Machine
Lumalabs is an innovative company that's been making waves in the AI and machine learning world. One of their standout features is Dream Machine, which allows you to generate videos from text or images using cutting-edge AI. Now, we’ve got our hands on an unofficial RESTful API for this, and I’m going to show you how to wrap it using FastAPI. Whether you’re a developer looking to integrate AI video generation into your app or just want to play around with Luma’s capabilities, this guide will help you get started.
API Documentation: Luma API
2. Setting Up FastAPI
2.1. Installing FastAPI
First things first, we need to set up FastAPI. If you don’t have it installed yet, you can quickly get it up and running with pip. Just run:
pip install fastapi
You’ll also need an ASGI server to serve your FastAPI application, like uvicorn
:
pip install uvicorn
2.2. Creating a Basic FastAPI App
Let's start with a very basic FastAPI app to make sure everything is set up correctly. Create a new Python file called main.py
:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, Luma AI!"}
Now, run the server using uvicorn:
uvicorn main:app --reload
Head over to http://127.0.0.1:8000/
in your browser, and you should see the "Hello, Luma AI!" message.
3. Wrapping the Luma API
3.1. Setting Up Your POST Endpoint
Next, we’ll create a POST endpoint in FastAPI that wraps the Luma API for generating videos. The idea is to make a clean and simple interface for internal use.
Here’s how you can set up the endpoint:
import requests
from fastapi import FastAPI, Form
app = FastAPI()
LUMA_API_URL = "https://api.lumaapi.com/api/v1/generation/add"
LUMA_API_TOKEN = "sk-xxx" # Replace this with your actual token
@app.post("/generate-video/")
def generate_video(
prompt: str = Form(...),
callback_url: str = Form(...),
image_url: str = Form(None),
image_end_url: str = Form(None),
aspect_ratio: str = Form("16:9")
):
payload = {
'arg_prompt': prompt,
'callback': callback_url,
'arg_image_url': image_url,
'arg_image_end_url': image_end_url,
'arg_aspect_ratio': aspect_ratio
}
headers = {
'Authorization': f'Bearer {LUMA_API_TOKEN}'
}
response = requests.post(LUMA_API_URL, headers=headers, data=payload)
return response.json()
3.2. Breaking Down the Endpoint
Payload Preparation: We collect the necessary data like prompt, callback URL, and image URLs using FastAPI’s
Form
function. This helps create the form-data structure that the Luma API expects.Authorization: We include the Bearer token in the headers for secure access to the API.
Making the API Request: We then make a POST request to the Luma API and return the response as JSON.
4. Testing Your API
4.1. Using FastAPI's Built-in Docs
One of the great things about FastAPI is the automatically generated docs. Once you’ve got your app running, head to http://127.0.0.1:8000/docs
, and you’ll see a nice interactive documentation page.
4.2. Making a Test Request
You can easily test your new /generate-video/
endpoint right from the docs. Fill in the form fields with a sample prompt, callback URL, and optionally, image URLs, then hit "Execute" to see the result.
5. Handling the Callback
5.1. Setting Up the Callback Endpoint
After the video is generated, Luma's API will send a POST request to the callback URL you provided. Let's set up an endpoint to handle that:
from fastapi import Request
@app.post("/api/cb_task/")
async def handle_callback(request: Request):
callback_data = await request.json()
# Do something with the callback data, like saving the video URL to a database
return {"status": "Callback received", "data": callback_data}
5.2. Processing the Callback Data
In the callback handler, you can process the data however you like. For example, you might want to store the video URL in your database or trigger another action based on the response.
6. Wrapping Up
You’ve just wrapped the Luma API using FastAPI, making it super easy to integrate AI-powered video generation into your projects. Whether you’re working on a large application or just exploring what’s possible with AI, this setup gives you a powerful tool at your fingertips. Happy coding!