Skip to main content

NodeJS

Learn how to create custom clips via Resemble platform by building a small command line application in 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-static-audio/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 an Clip you will need the UUID of both the Project and Voice you want to use to synthesize a 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, clip title, clip body and whether or not this clip is archived using input arguments. For example:

node index.js <project_uuid> <voice_uuid> <clip_title> <clip_body> <is_archived>

To accomplish this, we can use Node’s process.argv property which returns an array containing the command line arguments passed when the Node process was run. Append 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 !== 5) {
console.error('Usage: node index.js <project_uuid> <voice_uuid> <title> <body> <archived (true/false)>');
process.exit(1);
}

const [projectUUID, voiceUUID, title, body, isArchivedStr] = args;
const isArchived = isArchivedStr === 'true';

// Command-line arguments
const clipArgs = {
project_uuid: projectUUID,
voice_uuid: voiceUUID,
title: title,
body: body,
archived: isArchived,
};

In the above code snippet, we validate that the provided number of arguments is sufficient ① and proceed to de-structure ② and repackage the arguments for our script use ③. With the argument parsing in place, create a new function called createAudioClip responsible for initiating the clip creation process via the Resemble SDK using the synchronous execution.

import * as Resemble from '@resemble/node'

// -- snipped --

async function createAudioClip(projectUUID, title, body, voiceUUID, isArchived = false) {
console.log(`Submitting request to Resemble to create audio clip content: ${body}`)

try {
const response = await Resemble.Resemble.v2.clips.createSync(projectUUID, {
title: title,
voice_uuid: voiceUUID,
body: body,
is_archived: isArchived,
})

if (response.success) {
const clip = response.item
const clipUUID = clip.uuid
const clipURL = clip.audio_src

console.log(`Response was successful! ${title} has been created with UUID ${clipUUID}.`)
console.log(clipURL)
} else {
console.log('Response was unsuccessful!')
console.log(response)
}
} catch (error) {
console.error('Error creating audio clip:', error)
}
}

// -- snipped --

const [projectUUID, voiceUUID, title, body, isArchivedStr] = args
const isArchived = isArchivedStr === 'true'

const clipArgs = {
project_uuid: projectUUID,
voice_uuid: voiceUUID,
title: title,
body: body,
archived: isArchived,
}

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 title = args.title
const body = args.body
const isArchived = args.archived || false

// Run the clip creation function to call the Resemble API
await createAudioClip(projectUUID, title, body, voiceUUID, isArchived)
}

// -- snipped --

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

// -- snipped -

const clipArgs = {
project_uuid: projectUUID,
voice_uuid: voiceUUID,
title: title,
body: body,
archived: isArchived,
}

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

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" "Title" "Body" false false

Setting Resemble API Key...
Submitting request to Resemble to create audio clip content: Body
Response was successful! Title has been created with UUID f2859c2b.

Congratulations, you have successfully created a Clip via the API you can download it via the provided URL or use the web application to view your new clip.