Official Python SDK for Deepgram. Power your apps with world-class speech and Language AI models.
This SDK only supports hosted usage of api.deepgram.com.
- Deepgram Python SDK
- Documentation
- Getting an API Key
- Installation
- Examples
- Testing
- Configuration
- [Custom API Endpoint] [#custom-api-endpoint]
- Transcription
- Projects
- Keys
- Members
- Scopes
- Invitations
- Usage
- Billing
- Development and Contributing
- Getting Help
You can learn more about the Deepgram API at developers.deepgram.com.
🔑 To access the Deepgram API you will need a free Deepgram API Key.
pip install deepgram-sdkTo quickly get started with examples for prerecorded and streaming, run the files in the example folder. See the README in that folder for more information on getting started.
Run the following command to install pytest and pytest-cov as dev dependencies.
pip install -r requirements.txtpip install pytest
pip install pytest-covpytest --api-key <key> tests/pytest --cov=deepgram --api-key <key> tests/To setup the configuration of the Deepgram Client, do the following:
- Import the Deepgram client
- Create a Deepgram Client instance and pass in credentials to the constructor.
from deepgram import Deepgram
DEEPGRAM_API_KEY = 'YOUR_API_KEY'
deepgram = Deepgram(DEEPGRAM_API_KEY)In order to point the SDK at a different API environment (e.g., for on-prem deployments), you can pass in an object setting the api_url when initializing the Deepgram client.
dg_client = Deepgram({
"api_key": DEEPGRAM_API_KEY,
"api_url": "http://localhost:8080/v1/listen"
})from deepgram import Deepgram
import asyncio, json
DEEPGRAM_API_KEY = 'YOUR_API_KEY'
FILE_URL = 'https://static.deepgram.com/examples/interview_speech-analytics.wav'
async def main():
# Initializes the Deepgram SDK
deepgram = Deepgram(DEEPGRAM_API_KEY)
source = {'url': FILE_URL}
response = await asyncio.create_task(
deepgram.transcription.prerecorded(source, {
'smart_format': True,
'model': 'nova',
}))
print(json.dumps(response, indent=4))
asyncio.run(main())from deepgram import Deepgram
import json
DEEPGRAM_API_KEY = 'YOUR_API_KEY'
PATH_TO_FILE = 'some/file.wav'
def main():
# Initializes the Deepgram SDK
deepgram = Deepgram(DEEPGRAM_API_KEY)
# Open the audio file
with open(PATH_TO_FILE, 'rb') as audio:
# ...or replace mimetype as appropriate
source = {'buffer': audio, 'mimetype': 'audio/wav'}
response = deepgram.transcription.sync_prerecorded(source, {'punctuate': True})
print(json.dumps(response, indent=4))
main()from deepgram import Deepgram
import asyncio
import aiohttp
# Your Deepgram API Key
DEEPGRAM_API_KEY = 'YOUR_API_KEY'
# URL for the audio you would like to stream
URL = 'http://stream.live.vc.bbcmedia.co.uk/bbc_world_service'
async def main():
# Initialize the Deepgram SDK
deepgram = Deepgram(DEEPGRAM_API_KEY)
# Create a websocket connection to Deepgram
# In this example, punctuation is turned on, interim results are turned off, and language is set to US English.
try:
deepgramLive = await deepgram.transcription.live({ 'punctuate': True, 'interim_results': False, 'language': 'en-US' })
except Exception as e:
print(f'Could not open socket: {e}')
return
# Listen for the connection to close
deepgramLive.registerHandler(deepgramLive.event.CLOSE, lambda c: print(f'Connection closed with code {c}.'))
# Listen for any transcripts received from Deepgram and write them to the console
deepgramLive.registerHandler(deepgramLive.event.TRANSCRIPT_RECEIVED, print)
# Listen for the connection to open and send streaming audio from the URL to Deepgram
async with aiohttp.ClientSession() as session:
async with session.get(URL) as audio:
while True:
data = await audio.content.readany()
deepgramLive.send(data)
# If there's no data coming from the livestream then break out of the loop
if not data:
break
# Indicate that we've finished sending data by sending the customary zero-byte message to the Deepgram streaming endpoint, and wait until we get back the final summary metadata object
await deepgramLive.finish()
asyncio.run(main())Query parameters like punctuate are added as part of the TranscriptionOptions dict in the .prerecorded/.live transcription call.
Multiple query parameters can be added similarly, and any dict will do - the types are provided for reference/convenience.
response = await dg_client.transcription.prerecorded(source, {'punctuate': True, 'keywords': ['first:5', 'second']})Depending on your preference, you can also add parameters as named arguments, instead.
response = await dg_client.transcription.prerecorded(source, punctuate=True, keywords=['first:5', 'second'])The Deepgram.projects object provides access to manage projects associated with the API key you provided when instantiating the Deepgram client.
projects = await deepgram.projects.list(){
projects: [
{
project_id: String,
name: String,
},
],
}Retrieves a project based on the provided project id.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project to retrieve |
project = await deepgram.projects.get(PROJECT_ID){
project_id: String,
name: String,
}Updates a project based on a provided project object.
| Parameter | Type | Description |
|---|---|---|
| project | Object | Object representing a project. Must contain project_id and name properties. |
updateResponse = await deepgram.projects.update(project){
message: String;
}The Deepgram.keys object provides access to manage keys associated with your projects. Every function provided will required a PROJECT_ID that your current has access to manage.
You can retrieve all keys for a given project using the keys.list function.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project the API key will be created for |
response = await deepgram.keys.list(PROJECT_ID){
api_keys: [
{
api_key_id: string,
comment: string,
created: string,
scopes: Array<string>,
},
];
}Create a new API key for a project using the keys.create function with a name for the key.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project the API key will be created for |
| comment_for_key | String | A comment to denote what the API key is for |
| scopes | Array | A permissions scopes of the key to create |
response = await deepgram.keys.create(PROJECT_ID, COMMENT_FOR_KEY, SCOPES){
api_key_id: string,
key: string,
comment: string,
created: string,
scopes: string[]
}Delete an existing API key using the keys.delete method with project id and key id to delete.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project the API key will be delete from |
| key_id | String | A unique identifier for the API key to delete |
await deepgram.keys.delete(PROJECT_ID, KEY_ID)The keys.delete function returns a void.
The deepgram.members object provides access to the members endpoints of the Deepgram API. Each request is project based and will require a project_id.
You can retrieve all members on a given project using the members.list_members function.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to see the members of |
response = await deepgram.members.list_members(PROJECT_ID){
members: [
{
member_id: string,
scopes: Array<string>
email: string,
first_name: string,
last_name: string,
},
];
}You can remove a member from a given project using the members.remove_member function.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to see the members of |
| member_id | String | A unique identifier for the member you wish to remove |
response = await deepgram.members.remove_member(PROJECT_ID, MEMBER_ID){
message: string;
}The deepgram.scopes object provides access to the scopes endpoints of the Deepgram API. Each request is project based and will require a project_id.
You can retrieve all scopes of a member on a given project using the scopes.get_scope function.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to see the member scopes of |
| member_id | String | A unique identifier for the member you wish to see member scopes of |
response = await deepgram.scopes.get_scope(PROJECT_ID, MEMBER_ID){
scopes: string[]
}You can update the scope of a member on a given project using the scopes.update_scope function.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to update the member scopes of |
| member_id | String | A unique identifier for the member you wish to update member scopes of |
| scopes | String | The scope you wish to update the member to |
response = await deepgram.scopes.update_scope(PROJECT_ID, MEMBER_ID, 'member'){
message: string;
}The deepgram.invitations object provides access to the invites endpoints of the Deepgram API. Each request is project based and will require a project_id.
You can retrieve all active invitations on a given project using the invitations.list_invitations function.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to see the invitations of |
response = await deepgram.invitations.list_invitations(PROJECT_ID){
members: [
{
email: string,
scope: string,
},
];
}You can send an invitation to a given email address to join a given project using the invitations.send_invitation function.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to send an invitation from |
| option | Object | An object containing the email address of the person you wish to send an invite to, and the scope you want them to have in the project |
response = await deepgram.invitations.send_invitation(PROJECT_ID, {
email: 'example@email.com',
scope: 'member',
}){
message: string;
}Removes the invitation of the specified email from the specified project.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to remove the invite from |
| String | The email address of the invitee |
response = await deepgram.invitations.remove_invitation(
PROJECT_ID,
'example@email.com'
){
message: string;
}Removes the authenticated account from the specified project.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to leave |
response = await deepgram.invitations.leave_project(PROJECT_ID){
message: string;
}The deepgram.usage object provides access to the usage endpoints of the Deepgram API. Each request is project based and will require a project_id.
Retrieves transcription requests for a project based on the provided options.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project to retrieve usage for |
| options | Object | Parameters to filter requests. See below. |
{
// The time to retrieve requests made since
// Example: "2020-01-01T00:00:00+00:00"
start?: String,
// The time to retrieve requests made until
// Example: "2021-01-01T00:00:00+00:00"
end?: String,
// Page of requests to return
// Defaults to 0
page?: Number,
// Number of requests to return per page
// Defaults to 10. Maximum of 100
limit?: Number,
// Filter by succeeded or failed requests
// By default, all requests are returned
status?: 'succeeded' | 'failed'
}response = await deepgram.usage.list_requests(PROJECT_ID, {
'limit': 10,
# other options are available
}){
page: Number,
limit: Number,
requests?: [
{
request_id: String;
created: String;
path: String;
accessor: String;
response?: {
details: {
usd: Number;
duration: Number;
total_audio: Number;
channels: Number;
streams: Number;
model: String;
method: String;
tags: String[];
features: String[];
config: {
multichannel?: Boolean;
interim_results?: Boolean;
punctuate?: Boolean;
ner?: Boolean;
utterances?: Boolean;
replace?: Boolean;
profanity_filter?: Boolean;
keywords?: Boolean;
diarize?: Boolean;
search?: Boolean;
redact?: Boolean;
alternatives?: Boolean;
numerals?: Boolean;
};
}
}, ||
{
message?: String;
},
callback?: {
code: Number;
completed: String;
},
},
];
}Retrieves a specific transcription request for a project based on the provided projectId and requestId.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project to retrieve usage for |
| request_id | String | Unique identifier of the request to retrieve |
response = await deepgram.usage.get_request(PROJECT_ID, REQUEST_ID){
request_id: String;
created: String;
path: String;
accessor: String;
response?: {
details: {
usd: Number;
duration: Number;
total_audio: Number;
channels: Number;
streams: Number;
model: String;
method: String;
tags: String[];
features: String[];
config: {
multichannel?: Boolean;
interim_results?: Boolean;
punctuate?: Boolean;
ner?: Boolean;
utterances?: Boolean;
replace?: Boolean;
profanity_filter?: Boolean;
keywords?: Boolean;
diarize?: Boolean;
search?: Boolean;
redact?: Boolean;
alternatives?: Boolean;
numerals?: Boolean;
};
}
}, ||
{
message?: String;
},
callback?: {
code: Number;
completed: String;
}
}Retrieves aggregated usage data for a project based on the provided options.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project to retrieve usage for |
| options | Object | Parameters to filter requests. See below. |
{
// The time to retrieve requests made since
// Example: "2020-01-01T00:00:00+00:00"
start?: String,
// The time to retrieve requests made until
// Example: "2021-01-01T00:00:00+00:00"
end?: String,
// Specific identifer for a request
accessor?: String,
// Array of tags used in requests
tag?: String[],
// Filter requests by method
method?: "sync" | "async" | "streaming",
// Filter requests by model used
model?: String,
// Filter only requests using multichannel feature
multichannel?: Boolean,
// Filter only requests using interim results feature
interim_results?: Boolean,
// Filter only requests using the punctuation feature
punctuate?: Boolean,
// Filter only requests using ner feature
ner?: Boolean,
// Filter only requests using utterances feature
utterances?: Boolean,
// Filter only requests using replace feature
replace?: Boolean,
// Filter only requests using profanity_filter feature
profanity_filter?: Boolean,
// Filter only requests using keywords feature
keywords?: Boolean,
// Filter only requests using diarization feature
diarize?: Boolean,
// Filter only requests using search feature
search?: Boolean,
// Filter only requests using redact feature
redact?: Boolean,
// Filter only requests using alternatives feature
alternatives?: Boolean,
// Filter only requests using numerals feature
numerals?: Boolean
}response = await deepgram.usage.get_usage(PROJECT_ID, {
'start': '2020-01-01T00:00:00+00:00',
# other options are available
}){
start: String,
end: String,
resolution: {
units: String,
amount: Number
};
results: [
{
start: String,
end: String,
hours: Number,
requests: Number
}
];
}Retrieves features used by the provided project_id based on the provided options.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project to retrieve fields used for |
| options | Object | Parameters to filter requests. See below. |
{
// The time to retrieve requests made since
// Example: "2020-01-01T00:00:00+00:00"
start?: String,
// The time to retrieve requests made until
// Example: "2021-01-01T00:00:00+00:00"
end?: String
}response = await deepgram.usage.get_fields(PROJECT_ID, {
'start': '2020-01-01T00:00:00+00:00',
# other options are available
}){
tags: String[],
models: String[],
processing_methods: String[],
languages: String[],
features: String[]
}The deepgram.billing object provides access to the balances endpoints of the Deepgram API. Each request is project based and will require a project_id.
You can retrieve all balances on a given project using the billing.list_balance function.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to see the balance info of |
response = await deepgram.billing.list_balance(PROJECT_ID){
balances: [
{
balance_id: string
amount: number
units: string
purchase: string
}
]
}You can retrieve all balances on a given project using the billing.get_balance function.
| Parameter | Type | Description |
|---|---|---|
| project_id | String | A unique identifier for the project you wish to see the balance info of |
| balance_id | String | A unique identifier for the balance you wish to see the balance info of |
const response = deepgram.billing.get_balance(PROJECT_ID, BALANCE_ID){
balance: {
balance_id: string;
amount: number;
units: string;
purchase: string;
}
}Interested in contributing? We ❤️ pull requests!
To make sure our community is safe for all, be sure to review and agree to our Code of Conduct. Then see the Contribution guidelines for more information.
We love to hear from you so if you have questions, comments or find a bug in the project, let us know! You can either: