To set up the Deepgram API in an Angular application, you'll need to follow these steps:
- Install the Deepgram SDK:
First, install the Deepgram JavaScript SDK using npm:
npm install @deepgram/sdk
- Import and initialize the Deepgram client:
In your Angular component or service, import and initialize the Deepgram client:
import { createClient } from '@deepgram/sdk';
const deepgram = createClient('YOUR_DEEPGRAM_API_KEY');
- Create a WebSocket connection:
To use the live transcription feature, you'll need to create a WebSocket connection:
const connection = deepgram.listen.live({
model: 'nova',
punctuate: true,
});
- Handle WebSocket events:
Set up event listeners for the WebSocket connection:
connection.on('open', () => {
console.log('WebSocket connection established');
});
connection.on('transcriptReceived', (data) => {
console.log('Transcript:', data);
});
connection.on('error', (error) => {
console.error('Error:', error);
});
connection.on('close', () => {
console.log('WebSocket connection closed');
});
- Send audio data:
When you have audio data to send (e.g., from a microphone), you can send it to Deepgram:
connection.send(audioData);
Remember to handle the WebSocket connection lifecycle appropriately in your Angular component or service. You may want to create and close the connection based on the component's lifecycle hooks (e.g., ngOnInit and ngOnDestroy).
For more detailed information on using the Deepgram SDK in a web environment, you can refer to the Deepgram JavaScript SDK documentation (https://developers.deepgram.com/docs/node-sdk-streaming-transcription).