pub

Getting Started with Luma AI API Using Node.js and Axios

How to Use Luma AI API with Node.js and Axios

Lumalabs has been making waves with its Dream Machine feature, which lets you create amazing video content using AI. And guess what? You can tap into this magic using their API! In this guide, I’ll show you how to use Node.js and Axios to wrap Luma AI’s API for easy internal use.

1. What is Lumalabs and Dream Machine?

Before we dive into the code, let’s talk a little about Lumalabs. They’re the geniuses behind Dream Machine, a powerful AI tool that generates videos based on text prompts and images. Whether you're a creative professional or just someone who loves tinkering with AI, Dream Machine opens up a world of possibilities.

2. Setting Up Your Node.js Project

Let’s start by setting up a Node.js project. If you haven’t already, create a new directory for your project and run:

npm init -y

This will set up a basic Node.js project. Next, you’ll need to install Axios for making HTTP requests:

npm install axios form-data

We’ll use Axios to handle the requests to Luma’s API.

3. Wrapping Luma API with Axios

Alright, now for the fun part! Let’s create a simple wrapper function using Axios. Here’s a quick rundown of the code:

const axios = require('axios');
const FormData = require('form-data');

let data = new FormData();
data.append('arg_prompt', 'wind blows');
data.append('callback', 'http://127.0.0.1:3001/api/cb_task');

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.lumaapi.com/api/v1/generation/add',
  headers: {
    ...data.getHeaders()
  },
  data : data
};

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});

3.1 Understanding the Code

  • FormData: We’re using FormData to handle the request body, which will include the prompt and callback URL.
  • Axios Request: The axios.request() method sends the POST request to the Luma API. If everything goes smoothly, it logs the response; if not, it catches and logs the error.

4. Handling the Response

Once the request is successful, Luma’s API will return a response with a task ID. Here’s an example of what you might get:

{
    "code": 0,
    "msg": "Ok",
    "data": {
        "task_id": "la1234-5678-xxxxxx"
    }
}

4.1 What to Do with the Task ID?

The task ID is your ticket to retrieving the generated video. Luma will process your request and then post the result to the callback URL you provided.

5. Handling the Callback

Let’s talk about the callback. When Luma’s done processing, it’ll send the result to your server at the URL you specified. Here’s an example of what the data might look like:

{
  "code": 0,
  "msg": "string",
  "detail": "string",
  "data": {
    "task_id": "string",
    "payload": {},
    "result": {
      "id": "string",
      "prompt": "string",
      "created_at": "2024-06-22T13:57:05.878000Z",
      "video": {
        "url": "string",
        "download_video_url": "string",
        "thumbnail": "string",
        "width": 0,
        "height": 0
      }
    }
  }
}

5.1 Setting Up Your Callback Server

To handle this, you’ll need a server running at the callback URL. A simple Node.js Express server would work great for this. Here’s a basic example:

const express = require('express');
const app = express();

app.use(express.json());

app.post('/api/cb_task', (req, res) => {
  const data = req.body;
  console.log('Callback received:', data);
  res.sendStatus(200);
});

app.listen(3001, () => {
  console.log('Server running on port 3001');
});

6. Conclusion

And there you have it! You’ve just learned how to integrate Luma AI’s API with Node.js using Axios. This is just the tip of the iceberg; you can build on this to create more advanced features and really unlock the potential of Luma’s Dream Machine.

Happy coding!