Skip to content

Commit 1200c83

Browse files
authored
feat(auth): adding token-based auth to the SDK so access tokens can be used in requests (#543)
* feat(tests): create tests for our auth, so we know nothing breaks when we update it * feat(auth): update auth to support access token as well as api key * chore(tests): git ignore test outputted artifacts * chore(auth): add example for access tokens being ran * chore(docs): improve auth section for new access token * feat: resolve conflicts on readme * fix(tests): fixing broken tests * chore(tests): update snapshots * fix(auth): use correct precedence for new api key / access token work
1 parent a2d66fe commit 1200c83

File tree

38 files changed

+1140
-286
lines changed

38 files changed

+1140
-286
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ poetry.lock
2424
# examples
2525
chatlog.txt
2626
output_*.wav
27+
28+
# test artifacts
29+
output.wav
30+
test.mp3

README.md

Lines changed: 108 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Official Python SDK for [Deepgram](https://www.deepgram.com/). Power your apps w
88
- [Documentation](#documentation)
99
- [Migrating from earlier versions](#migrating-from-earlier-versions)
1010
- [V2 to V3](#v2-to-v3)
11-
- [V3.*\ to V4](#v3-to-v4)
11+
- [V3.\*\ to V4](#v3-to-v4)
1212
- [Requirements](#requirements)
1313
- [Installation](#installation)
1414
- [Initialization](#initialization)
@@ -129,8 +129,6 @@ response = deepgram.listen.rest.v("1").transcribe_url(
129129

130130
[See our API reference for more info](https://developers.deepgram.com/reference/speech-to-text-api/listen).
131131

132-
[See the Example for more info](./examples/speech-to-text/rest/sync/url/main.py).
133-
134132
### Local Files (Synchronous)
135133

136134
Transcribe audio from a file.
@@ -146,8 +144,6 @@ response = deepgram.listen.rest.v("1").transcribe_file(
146144

147145
[See our API reference for more info](https://developers.deepgram.com/reference/speech-to-text-api/listen).
148146

149-
[See the Example for more info](./examples/speech-to-text/rest/sync/file/main.py).
150-
151147
## Pre-Recorded (Asynchronous / Callbacks)
152148

153149
### Remote Files (Asynchronous)
@@ -166,8 +162,6 @@ response = deepgram.listen.rest.v("1").transcribe_url_async(
166162

167163
[See our API reference for more info](https://developers.deepgram.com/reference/speech-to-text-api/listen).
168164

169-
[See the Example for more info](./examples/speech-to-text/rest/async/url/main.py).
170-
171165
### Local Files (Asynchronous)
172166

173167
Transcribe audio from a file.
@@ -184,8 +178,6 @@ response = deepgram.listen.rest.v("1").transcribe_file_async(
184178

185179
[See our API reference for more info](https://developers.deepgram.com/reference/speech-to-text-api/listen).
186180

187-
[See the Example for more info](./examples/speech-to-text/rest/async/file/main.py).
188-
189181
## Streaming Audio
190182

191183
Transcribe streaming audio.
@@ -213,8 +205,6 @@ connection.finish()
213205

214206
[See our API reference for more info](https://developers.deepgram.com/reference/streaming-api).
215207

216-
[See the Examples for more info](./examples/speech-to-text/websocket/).
217-
218208
## Transcribing to Captions
219209

220210
Transcribe audio to captions.
@@ -287,8 +277,6 @@ For a complete implementation, you would need to:
287277

288278
[See our API reference for more info](https://developers.deepgram.com/reference/voice-agent-api/agent).
289279

290-
[See the Examples for more info](./examples/agent/).
291-
292280
## Text to Speech REST
293281

294282
Convert text into speech using the REST API.
@@ -309,8 +297,6 @@ response = deepgram.speak.rest.v("1").save(
309297

310298
[See our API reference for more info](https://developers.deepgram.com/reference/text-to-speech-api/speak).
311299

312-
[See the Example for more info](./examples/text-to-speech/rest/).
313-
314300
## Text to Speech Streaming
315301

316302
Convert streaming text into speech using a Websocket.
@@ -346,8 +332,6 @@ connection.finish()
346332

347333
[See our API reference for more info](https://developers.deepgram.com/reference/text-to-speech-api/speak).
348334

349-
[See the Examples for more info](./examples/text-to-speech/websocket/).
350-
351335
## Text Intelligence
352336

353337
Analyze text.
@@ -372,28 +356,126 @@ response = deepgram.read.rest.v("1").process(
372356

373357
## Authentication
374358

375-
### Get Token Details
359+
The Deepgram Python SDK supports multiple authentication methods to provide flexibility and enhanced security for your applications.
360+
361+
### Authentication Methods
376362

377-
Retrieves the details of the current authentication token.
363+
#### API Key Authentication (Traditional)
364+
365+
The traditional method using your Deepgram API key:
378366

379367
```python
380-
response = deepgram.manage.rest.v("1").get_token_details()
368+
from deepgram import DeepgramClient
369+
370+
# Direct API key
371+
client = DeepgramClient(api_key="YOUR_API_KEY")
372+
373+
# Or using environment variable DEEPGRAM_API_KEY
374+
client = DeepgramClient() # Auto-detects from environment
375+
```
376+
377+
#### Bearer Token Authentication (OAuth 2.0)
378+
379+
Use short-lived access tokens for enhanced security:
380+
381+
```python
382+
from deepgram import DeepgramClient
383+
384+
# Direct access token
385+
client = DeepgramClient(access_token="YOUR_ACCESS_TOKEN")
386+
387+
# Or using environment variable DEEPGRAM_ACCESS_TOKEN
388+
client = DeepgramClient() # Auto-detects from environment
381389
```
382390

383-
[See our API reference for more info](https://developers.deepgram.com/reference/authentication).
391+
### Authentication Priority
392+
393+
When multiple credentials are provided, the SDK follows this priority order:
394+
395+
1. **Explicit `access_token` parameter** (highest priority)
396+
2. **Explicit `api_key` parameter**
397+
3. **`DEEPGRAM_ACCESS_TOKEN` environment variable**
398+
4. **`DEEPGRAM_API_KEY` environment variable** (lowest priority)
399+
400+
### Environment Variables
401+
402+
Set your credentials using environment variables:
403+
404+
```bash
405+
# For API key authentication
406+
export DEEPGRAM_API_KEY="your-deepgram-api-key"
407+
408+
# For bearer token authentication
409+
export DEEPGRAM_ACCESS_TOKEN="your-access-token"
410+
```
384411

385-
### Grant Token
412+
### Dynamic Authentication Switching
386413

387-
Creates a temporary token with a 30-second TTL.
414+
Switch between authentication methods at runtime:
388415

389416
```python
390-
response = deepgram.auth.v("1").grant_token()
417+
from deepgram import DeepgramClient, DeepgramClientOptions
418+
419+
# Start with API key
420+
config = DeepgramClientOptions(api_key="your-api-key")
421+
client = DeepgramClient(config=config)
391422

423+
# Switch to access token
424+
client._config.set_access_token("your-access-token")
425+
426+
# Switch back to API key
427+
client._config.set_apikey("your-api-key")
392428
```
393429

394-
[See our API reference for more info](https://developers.deepgram.com/reference/token-based-auth-api/grant-token).
430+
### Complete Bearer Token Workflow
431+
432+
Here's a practical example of using API keys to obtain access tokens:
433+
434+
```python
435+
from deepgram import DeepgramClient
436+
437+
# Step 1: Create client with API key
438+
api_client = DeepgramClient(api_key="your-api-key")
439+
440+
# Step 2: Get a short-lived access token (30-second TTL)
441+
response = api_client.auth.v("1").grant_token()
442+
access_token = response.access_token
443+
444+
# Step 3: Create new client with Bearer token
445+
bearer_client = DeepgramClient(access_token=access_token)
446+
447+
# Step 4: Use the Bearer client for API calls
448+
transcription = bearer_client.listen.rest.v("1").transcribe_url(
449+
{"url": "https://dpgr.am/spacewalk.wav"}
450+
)
451+
```
452+
453+
### Benefits of Bearer Token Authentication
454+
455+
- **Enhanced Security**: Short-lived tokens (30-second expiration) minimize risk
456+
- **OAuth 2.0 Compliance**: Standard bearer token format
457+
- **Scope Limitation**: Tokens can be scoped to specific permissions
458+
- **Audit Trail**: Better tracking of token usage vs API keys
459+
460+
### Authentication Management
461+
462+
#### Get Token Details
463+
464+
Retrieves the details of the current authentication token:
465+
466+
```python
467+
response = deepgram.manage.rest.v("1").get_token_details()
468+
```
469+
470+
#### Grant Token
395471

396-
[See The Examples for more info](./examples/auth/)
472+
Creates a temporary token with a 30-second TTL:
473+
474+
```python
475+
response = deepgram.auth.v("1").grant_token()
476+
```
477+
478+
[See our API reference for more info](https://developers.deepgram.com/reference/token-based-auth-api/grant-token).
397479

398480
## Projects
399481

@@ -407,8 +489,6 @@ response = deepgram.manage.v("1").get_projects()
407489

408490
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/projects/list).
409491

410-
[See The Example for more info](./examples/manage/projects/main.py).
411-
412492
### Get Project
413493

414494
Retrieves a specific project based on the provided project_id.
@@ -419,8 +499,6 @@ response = deepgram.manage.v("1").get_project(myProjectId)
419499

420500
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/projects/get).
421501

422-
[See The Example for more info](./examples/manage/projects/main.py).
423-
424502
### Update Project
425503

426504
Update a project.
@@ -431,8 +509,6 @@ response = deepgram.manage.v("1").update_project(myProjectId, options)
431509

432510
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/projects/update).
433511

434-
[See The Example for more info](./examples/manage/projects/main.py).
435-
436512
### Delete Project
437513

438514
Delete a project.
@@ -443,8 +519,6 @@ response = deepgram.manage.v("1").delete_project(myProjectId)
443519

444520
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/projects/delete).
445521

446-
[See The Example for more info](./examples/manage/projects/main.py).
447-
448522
## Keys
449523

450524
### List Keys
@@ -457,8 +531,6 @@ response = deepgram.manage.v("1").get_keys(myProjectId)
457531

458532
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/keys/list)
459533

460-
[See The Example for more info](./examples/manage/keys/main.py).
461-
462534
### Get Key
463535

464536
Retrieves a specific key associated with the provided project_id.
@@ -469,8 +541,6 @@ response = deepgram.manage.v("1").get_key(myProjectId, myKeyId)
469541

470542
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/keys/get)
471543

472-
[See The Example for more info](./examples/manage/keys/main.py).
473-
474544
### Create Key
475545

476546
Creates an API key with the provided scopes.
@@ -481,8 +551,6 @@ Creates an API key with the provided scopes.
481551

482552
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/keys/create)
483553

484-
[See The Example for more info](./examples/manage/keys/main.py).
485-
486554
### Delete Key
487555

488556
Deletes a specific key associated with the provided project_id.
@@ -493,8 +561,6 @@ response = deepgram.manage.v("1").delete_key(myProjectId, myKeyId)
493561

494562
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/keys/delete)
495563

496-
[See The Example for more info](./examples/manage/keys/main.py).
497-
498564
## Members
499565

500566
### Get Members
@@ -507,8 +573,6 @@ response = deepgram.manage.v("1").get_members(myProjectId)
507573

508574
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/members/list).
509575

510-
[See The Example for more info](./examples/manage/members/main.py).
511-
512576
### Remove Member
513577

514578
Removes member account for specified member_id.
@@ -519,8 +583,6 @@ response = deepgram.manage.v("1").remove_member(myProjectId, MemberId)
519583

520584
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/members/delete).
521585

522-
[See The Example for more info](./examples/manage/members/main.py).
523-
524586
## Scopes
525587

526588
### Get Member Scopes
@@ -533,8 +595,6 @@ response = deepgram.manage.v("1").get_member_scopes(myProjectId, memberId)
533595

534596
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/scopes/list).
535597

536-
[See The Example for more info](./examples/manage/scopes/main.py).
537-
538598
### Update Scope
539599

540600
Updates the scope for the specified member in the specified project.
@@ -545,8 +605,6 @@ response = deepgram.manage.v("1").update_member_scope(myProjectId, memberId, opt
545605

546606
[See our API reference for more info](https://developers.deepgram.com/reference/management-api/scopes/update).
547607

548-
[See The Example for more info](./examples/manage/scopes/main.py).
549-
550608
## Invitations
551609

552610
### List Invites

0 commit comments

Comments
 (0)