#Since ElevenLabs stores every generated audio track in a speech history, can I access and download

1 messages · Page 1 of 1 (latest)

idle tuskBOT
#
AI-generated Answer for 7ven7o

Yes, you can access and download old speech tracks via an ID and the API. You can use the following endpoint to download history items:

POST /v1/history/download

You need to provide your API key and a list of history item IDs. Here is an example in Python:

from elevenlabs.client import ElevenLabs

client = ElevenLabs(api_key="YOUR_API_KEY")

client.history.download(
    history_item_ids=["your_history_item_id"]
)

You can get the IDs of history items and other metadata using the GET /v1/history endpoint.

south bane
#

How do I do this with typescript?

idle tuskBOT
#
AI-generated Answer for 7ven7o

To access and download old speech tracks via an ID using TypeScript, you can use the ElevenLabs API. Below is a basic example of how you might achieve this:

  1. Install the necessary packages:

    npm install axios
    
  2. Create a TypeScript file (e.g., downloadSpeech.ts):

    import axios from 'axios';
    
    const API_KEY = 'your_api_key_here';
    const BASE_URL = 'https://api.elevenlabs.io/v1';
    
    async function downloadSpeech(speechId: string) {
      try {
        const response = await axios.get(`${BASE_URL}/speech/${speechId}`, {
          headers: {
            'Authorization': `Bearer ${API_KEY}`
          },
          responseType: 'arraybuffer'
        });
    
        const audioData = response.data;
        const fs = require('fs');
        fs.writeFileSync(`./${speechId}.mp3`, audioData);
        console.log('Download complete');
      } catch (error) {
        console.error('Error downloading speech:', error);
      }
    }
    
    // Replace 'your_speech_id_here' with the actual speech ID you want to download
    downloadSpeech('your_speech_id_here');
    
  3. Run the script:

    npx ts-node downloadSpeech.ts
    

Make sure to replace 'your_api_key_here' and 'your_speech_id_here' with your actual API key and the speech ID you want to download. This script will download the specified speech track and save it as an MP3 file.

south bane
#

Can I use the JS/TS ElevenLabs library to do this?

idle tuskBOT
#
AI-generated Answer for 7ven7o

Yes, you can use the ElevenLabs JavaScript/TypeScript library to download old speech tracks via an ID and the API. Here is an example of how to do this:

import { ElevenLabs } from 'elevenlabs';

const client = new ElevenLabs({
  apiKey: 'YOUR_API_KEY',
});

async function downloadHistoryItem() {
  try {
    const historyItemIds = ['your_history_item_id']; // Replace with your actual history item ID
    const response = await client.history.download({
      history_item_ids: historyItemIds,
    });
    console.log('Download successful:', response);
  } catch (error) {
    console.error('Error downloading history item:', error);
  }
}

downloadHistoryItem();

Make sure to replace 'YOUR_API_KEY' with your actual API key and 'your_history_item_id' with the actual ID of the history item you want to download.

south bane
#

Awesome! Now if I request audio like this:

// Function to generate audio stream from ElevenLabs
async function elevenLabsAudioStream(text: string, voice: string): Promise<AsyncIterable<Buffer>> {
  // clg(`Making ElevenLabs request\n    text: ${request.text}\n    voice: ${request.voice}`)
  const audioStream = await client.generate({
    voice: voice,
    model_id: 'eleven_multilingual_v2',
    text: text,
  });
  return audioStream;
}

Is there a way I can get the history_item_id of this new audio track at the same time?