Skip to main content

NodeJS

Learn how to build your own custom voice using a dataset through the Resemble AI platform 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
  • Have a subscription to at least a PRO on the Resemble platform.

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 which will be used for this example.

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

# resemble-clone-voice/index.js

import * as Resemble from "@resemble/node"
import fs from 'fs'

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)

Notice that the function expects the environment variable RESEMBLE_API_KEY to be present containing the necessary API key.

Lets now build the createVoice function responsible for cloning a voice. Add the following code snippet below to your resemble-clone-voice/index.js file.

// resemble-clone-voice/index.js

// -- snipped --

async function createVoice() {
const voiceName = 'My Voice Clone';

const dataset = 'FIXME: Adds your dataseturl here';


// Make a request to the API, note that we do not provide a callback_uri so this
// request will execute synchronously.

console.log(`Submitting request to Resemble to create a voice: ${voiceName}`);

try {
// Make a request to the API, note that we do not provide a callback_uri so this
// request will execute synchronously.

const response = await Resemble.Resemble.v2.voices.create({
name: voiceName,
dataset_url: dataset,
});

const voice = response.item;

if (response.success) {
const voiceStatus = voice.status;
const voiceUuid = voice.uuid;

console.log(`Response was successful! ${voiceName} has been created with UUID ${voiceUuid}. The voice is currently ${voiceStatus}.`);
} else {
console.log('Response was unsuccessful!');
// In case of an error, print the error to console
console.log(response);
}
} catch (error) {
console.error('An error occurred:', error);
}
}

const response = await Resemble.Resemble.v2.voices.create({ page:1, page_size: 10 });

console.log(response)

There are two options for creating a Voice using the Resemble API:

  1. Providing a dataset URL; or
  2. Uploading individual recordings via the API

In this guide, we will use option #1: providing a dataset URL. If you’re interested in building a voice using option #2, you can see this guide.

In the above code snippet, the dataset ① must be set to a public accessible URL containing a supported Resemble dataset format. To avoid delays in Voice cloning, double check that your dataset URL is accessible via the curl package.

curl -I https://url/to/your/dataset.zip

Once you have updated the dataset value, execute the createVoice function.

// -- snipped --

// Execute the voice creation
createVoice()

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

# Be sure to prepend your API key to the command
RESEMBLE_API_KEY=... node index.js

# Output
Submitting request to Resemble to create a voice: My Voice Clone

{
"success": True,
"item": {
"uuid": 'abc1234',
"name": 'My Voice Clone',
"status": 'initializing',
# -- snipped --
}
}

Response was successful!

My Voice Clone has been created with UUID abc1234.

The voice is currently initializing.

Congratulations, your voice clone has begun building! To check the status of your voice, you can programmatically access via the API or use the web application as well.