AssemblyAI QuickStart
by juberti
JavaScript
// Welcome to AssemblyAI! Get started with the API by transcribing
// a file using JavaScript.
//
// In this example, we'll transcribe a local file. Get started by
// downloading the snippet, then update the 'filename' variable
// to point to the local path of the file you want to upload and
// use the API to transcribe.
//
// IMPORTANT: Update line 100 to point to a local file.
//
// Have fun!
// Your API token is already set in this variable
const API_TOKEN = "e59553fb27184b5ebbfee3d595d9545c";
// Function to upload a local file to the AssemblyAI API
async function upload_file(api_token, path) {
console.log(`Uploading file: ${path}`);
// Read the file data
const r1 = await fetch("https://archive.org/download/mantle_other_stories_2306_librivox/mantleandotherstories_00_gogol_128kb.mp3");
console.log(r1);
const data = await r1.blob();
console.log(data);
const url = "https://api.assemblyai.com/v2/upload";
try {
// Send a POST request to the API to upload the file, passing in the headers and the file data
const response = await fetch(url, {
method: "POST",
body: data,
headers: {
"Content-Type": "application/octet-stream",
Authorization: api_token,
},
});
// If the response is successful, return the upload URL
if (response.status === 200) {
const responseData = await response.json();
return responseData["upload_url"];
} else {
console.error(`Error: ${response.status} - ${response.statusText}`);
return null;
}
} catch (error) {
console.error(`Error: ${error}`);
return null;
}
}
// Async function that sends a request to the AssemblyAI transcription API and retrieves the transcript
async function transcribeAudio(api_token, audio_url) {
console.log("Transcribing audio... This might take a moment.");
// Set the headers for the request, including the API token and content type
const headers = {
authorization: api_token,
...