I've recently started doing a lot more work directly in my terminal - and I've learned that writing Bash scripts doesn't have to be scary. Today, we'll write a set of commands and scripts to execute directly in our terminal.
Before You Start
You will need a Deepgram API Key - get one here. You will also need to install jq.
Making a cURL Request
Open your terminal and type (or copy and paste) the following, not forgetting to change YOUR_DEEPGRAM_API_KEY with a real API Key, and then press enter:
Let's break down each part of this request:
--request POST: is a HTTP request with the POST method.
--header 'Authorization: Token YOUR_DEEPGRAM_API_KEY' - include details to link this request with our account/project.
--header 'Content-Type: application/json' - JSON data will be sent with this request.
--data '{"url":"https://static.deepgram.com/examples/nasa-spacewalk-interview.wav"}'. - is the JSON data sent to Deepgram (an object containing one url parameter).
--url 'https://api.deepgram.com/v1/listen?punctuate=true' - the URL to make the request to (Deepgram's endpoint). punctuate=true enables the punctuation feature.
\ allows us to break one command over several lines for readability.
Shortening Your Request
The first example is handy for understanding all of the required parameters. Here is a more concise way to make the same request:
The first thing you'll notice is that the URL comes immediately after the curl command. You may have also noted the absence of an HTTP method - this would normally default to a GET request, but as this request has a body, a POST request is inferred. --header is shortened to -H, and --data to -d.
Adding jq
jq is an excellent command-line utility that allows you to display and manipulate JSON data. On the terminal, a pipe (|) is often used to send the output of one command as an input for a second command. jq expects some JSON as input and an expression to describe how to display it.
This jq expression will extract just the transcript from the returned data object:
| jq '.results.channels[0].alternatives[0].transcript'
You can add it to the end of your cURL request like so:
Saving Output to File
Once you have the correct data extracted and formatted from jq, you can redirect the output to a new file by appending > output.txt to any command that prints to the terminal. Here it is in practice:
Processing Multiple Files
You can create .sh files to execute from your terminal, which contain multiple lines of bash script. Create a new file called transcripts.sh and open it in a code editor. Copy and paste the following:
Let's break down each part of this file:
The first line - #!/bin/bash - is a shebang, and specifies which program should be called to run the script. In this case, bash.
urls is a variable containing an array with three URLs. Notice that arrays use parentheses, and items are separated by a space only.
dg_features and dg_key are variables you should alter for your exact use case.
Inside of the for loop:
filename extracts the last part of the URL (the filename), which will later be used to name the output file.
The curl command is the same as before, with variables interpolated. The output is stored in a new variable called RESPONSE.
RESPONSE is sent to jq, and then redirected into a new text file.
Run the file in your terminal with ./transcripts.sh. As a note, this request uses Deepgram's punctuation, utterances, diarize, and tier features.
Playing with jq
jq is a remarkably powerful tool. The following expression will loop through the results.utterances array, and format a string for each item interpolating the speaker identifier and transcript text:
The output looks like this:
I hope you found this valuable and interesting. If you have any questions, please feel free to get in touch - we love to help!
If you have any feedback about this post, or anything else around Deepgram, we'd love to hear from you. Please let us know in our GitHub discussions .
Unlock language AI at scale with an API call.
Get conversational intelligence with transcription and understanding on the world's best speech AI platform.