Skip to content

Using the API

Before starting, make sure you have followed the instructions in the setup page.

Talking to the API

In this example, we’ll use the gen4_turbo model to generate a video from an image using the text prompt “Generate a video”. You’ll want to replace the promptImage with a URL of an image and a promptText with your own text prompt.

First, you’ll want to install the Runway SDK. You can do this with npm:

Terminal window
npm install --save @runwayml/sdk

In your code, you can now import the SDK and start making requests:

import RunwayML from '@runwayml/sdk';
const client = new RunwayML();
async function main() {
// Create a new image-to-video task using the "gen4_turbo" model
const imageToVideo = await client.imageToVideo.create({
model: 'gen4_turbo',
// Point this at your own image file
promptImage: 'https://example.com/image.jpg',
promptText: 'Generate a video',
ratio: '1280:720',
duration: 5,
});
const taskId = imageToVideo.id;
// Poll the task until it's complete
let task: Awaited<ReturnType<typeof client.tasks.retrieve>>;
do {
// Wait for ten seconds before polling
await new Promise(resolve => setTimeout(resolve, 10000));
task = await client.tasks.retrieve(taskId);
} while (!['SUCCEEDED', 'FAILED'].includes(task.status));
console.log('Task complete:', task);
}
main();

Uploading base64 encoded images as data URIs

You can also upload base64 encoded images (as a data URI) instead of pointing to an external URL. This can be useful if you’re working with a local image file and want to avoid an extra network round trip to upload the image.

To do this, simply pass the base64 encoded image string as a data URI in the promptImage field instead of a URL. For more information about file types and size limits, see the Inputs page.

import fs from 'fs';
import path from 'path';
import RunwayML from '@runwayml/sdk';
const client = new RunwayML();
// Sample image path - replace with your image path
const imagePath = 'example.png';
// Read the image file
const imageBuffer = fs.readFileSync(imagePath);
// Convert to base64
const base64String = imageBuffer.toString('base64');
async function main() {
// Create a new image-to-video task using the "gen4_turbo" model
const imageToVideo = await client.imageToVideo.create({
model: 'gen4_turbo',
// Point this at your own image file
promptImage: `data:image/${path.extname(imagePath).slice(1)};base64,${base64String}`,
promptText: 'Generate a video',
ratio: '1280:720',
duration: 5,
});
const taskId = imageToVideo.id;
// Poll the task until it's complete
let task: Awaited<ReturnType<typeof client.tasks.retrieve>>;
do {
// Wait for ten seconds before polling
await new Promise(resolve => setTimeout(resolve, 10000));
task = await client.tasks.retrieve(taskId);
} while (!['SUCCEEDED', 'FAILED'].includes(task.status));
console.log('Task complete:', task);
}
main();