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';

let base64Conset = '';

// In order to clone a voice, you MUST provide a base64 encoded consent file
//
// https://docs.app.resemble.ai/docs/create_voices/resource_voice/create#voice-consent
//
// FIXME: You will need update this function to the path to your consent file
const fileContents = fs.readFileSync('FIXME: path/to/consent file', 'binary');

base64Conset = Buffer.from(fileContents).toString('base64')


// 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,
consent: base64Conset
});

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

In commitment to Resemble's Ethical Statement all voice clone requests must be authorized via a consent file. You can read more about this in the Voice Consent section. In the above snippet, the code reads the provided consent file, encodes to base64 and provides in the request ②.

Once you have updated the dataset value, consent file path and validated access, 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.