Skip to main content
Version: 2.0.0

NodeJS

Learn how to create multiple clips using through the Resemble AI Batch API with NodeJS.

In order to best use this guide ensure that you have:

  • Signed up for a Resemble.ai account and confirmed your e-mail
  • Obtained your API key for use with the API - for additional information see the Authentication page

Guide

First, you will need to initialize your Node project

# Create node environment
npm init

With your project setup, we can install the Resemble SDK via npm install

# Install Resemble SDK
npm install @resemble/node

Create a new index.js file - this file will contain a small CLI program we can use to create a piece of audio content using the Resemble AI platform.

Import the Resemble class from the resemble sdk, and add the setupResembleAI function to initialize the Resemble API client.

// resemble-batch/index.js

import * as Resemble from '@resemble/node'

const apiKey = process.env.RESEMBLE_API_KEY

if (!apiKey) {
console.error('Please set the RESEMBLE_API_KEY environment variable.')
process.exit(1)
}

const setupResembleAI = (apiKey) => {
console.log('Setting Resemble API Key...')
Resemble.Resemble.setApiKey(apiKey)
}

setupResembleAI(apiKey)

The program expects the environment variable RESEMBLE_API_KEY to be present containing the necessary API key in order to initialize the Resemble AI client.

Every Clip created on the Resemble AI platform is associated with a Project and synthesized using a Voice. To create a Batch of clips you will need the UUID of both the Project and Voice you want to use synthesize each clip. You can refer to the Project and Voice documentation to learn how to find your Project and Voice UUID.

We want to build our program in way that the user can specify their desired Project, Voice, and a comma-separated list of clip content to synthesize, for example:

node index.js <project_uuid> <voice_uuid> <csv-of-clips>

We can use Node’s process.argv property which returns an array containing the command line arguments provided when the Node process was run. Add the following code snippet to your index.js file:

import * as Resemble from '@resemble/node'

// -- snipped --

const args = process.argv.slice(2);

// Check if the required number of arguments is provided
if (args.length !== 3) {
console.error('Usage: node index.js <project_uuid> <voice_uuid> <csv-of-clips>');
process.exit(1);
}

const [projectUUID, voiceUUID, clips] = args;

// Command-line arguments
const batchArgs = {
project_uuid: projectUUID,
voice_uuid: voiceUUID,
clips: clips,
};

Above, the script first validates that there is the correct number of arguments present ①, next it de-structures the arguments ② and repacks them for the runExample function to use ③.

Next, create a new function called batchCreateClips which will initiate a new Batch synthesis request using the Resemble SDK.

import * as Resemble from '@resemble/node'

// -- snipped --

async function batchCreateClips(projectUUID, voiceUUID, clipsArray) {
try {
const response = await Resemble.Resemble.v2.batch.create(projectUUID, voiceUUID, clipsArray, {})

if (response.success) {
const batch = response.item
const batchUUID = batch.uuid
const count = batch.total_count

console.log(`Response was successful! Batch of ${count} clips has been created with UUID ${batchUUID}.`)
} else {
console.log('Response was unsuccessful!')
console.log(response)
}
} catch (error) {
console.error('Error creating audio clip:', error)
}
}

// -- snipped --

const [projectUUID, voiceUUID, clips] = args

// Command-line arguments
const batchArgs = {
project_uuid: projectUUID,
voice_uuid: voiceUUID,
clips: clips,
}

Next, we can add a runExample function to tie together all the parts of our script:

// -- snipped --

// Main function
async function runExample(args) {
const projectUUID = args.project_uuid
const voiceUUID = args.voice_uuid
const clips = args.clips
const clipsArray = clips.split(',')

// Run the clip creation function to call the Resemble API
await batchCreateClips(projectUUID, voiceUUID, clipsArray)
}

// -- snipped --

Finally, add the a call to runExample at the bottom of the file with the clipArgs:

// -- snipped -

// Command-line arguments
const batchArgs = {
project_uuid: projectUUID,
voice_uuid: voiceUUID,
clips: clips,
}

// Call the 'runExample' function with the provided arguments
runExample(batchArgs)

Finally, test your script by running the command via CLI :

# Be sure to prepend your API key to the command
RESEMBLE_API_KEY=... node index.js "abcdef123" "321fedabc" "hi,hello,goodbye!"

Setting Resemble API Key...

Response was successful! Batch of 3 clips has been created with UUID deadbeef

Congratulations, you have successfully created a clip batch via the API. You can view the clip synthesis details in the response body, or alternatively navigating to your project in the web application.