Schedule a DELETE request with AssemblyAI and EasyCron

This tutorial guides you through the process of scheduling a DELETE request for a transcript created with AssemblyAI’s transcription service, utilizing EasyCron for scheduling.

Quickstart

1import assemblyai as aai
2import requests
3from datetime import datetime, timedelta, timezone
4
5# Set up AssemblyAI API key
6# In production, store this in an environment variable
7aai.settings.api_key = "YOUR_ASSEMBLYAI_API_KEY"
8
9# Get an EasyCron API key here: https://www.easycron.com/user/token
10# Set up EasyCron API key
11# In production, store this in an environment variable
12EASYCRON_API_TOKEN = "YOUR_EASYCRON_API_KEY"
13
14# Create transcriber instance and transcribe audio
15config = aai.TranscriptionConfig(speech_models=["universal-3-pro", "universal-2"])
16transcriber = aai.Transcriber()
17transcript = transcriber.transcribe('https://assembly.ai/sports_injuries.mp3', config)
18
19# Get the transcript ID
20transcript_id = transcript.id
21
22# Construct the URL for the DELETE request
23url_to_call = f"https://api.assemblyai.com/v2/transcript/{transcript_id}"
24
25# Calculate the time 24 hours from now for the cron expression
26time_24_hours_from_now = datetime.now(timezone.utc) + timedelta(hours=24)
27cron_minute = time_24_hours_from_now.minute
28cron_hour = time_24_hours_from_now.hour
29cron_day = time_24_hours_from_now.day
30cron_month = time_24_hours_from_now.month
31cron_year = time_24_hours_from_now.year
32
33# Create the cron expression
34cron_expression = f'{cron_minute} {cron_hour} {cron_day} {cron_month} * {cron_year}'
35
36# EasyCron API endpoint for creating a new cron job
37api_endpoint = 'https://www.easycron.com/rest/add'
38
39# Data payload for EasyCron API
40data = {
41 'token': EASYCRON_API_TOKEN,
42 'url': url_to_call,
43 'cron_expression': cron_expression,
44 'http_method': "DELETE",
45 'http_headers': f'Authorization: {aai.settings.api_key}',
46 'timezone': 'UTC'
47}
48
49# Make the request to EasyCron's API
50response = requests.post(api_endpoint, data=data)
51
52# Print the response from EasyCron
53print("EasyCron API Response:")
54print(response.text)

Step-by-step guide

To get started, install the AssemblyAI Python SDK.

$pip install "assemblyai"

Finally, import the assemblyai package and set your API token in the settings:

1import assemblyai as aai
2
3# set the API key
4aai.settings.api_key = f"{YOUR_API_KEY}"
1config = aai.TranscriptionConfig(speech_models=["universal-3-pro", "universal-2"])
2transcriber = aai.Transcriber()
3
4# this is just an example file
5transcript = transcriber.transcribe('https://assembly.ai/sports_injuries.mp3', config)

Store the transcript ID

1transcript_id = transcript.id

Using this, we now construct the URL that we want our cron job to call.

1url_to_call = f"https://api.assemblyai.com/v2/transcript/{transcript_id}"

Using EasyCron’s API to schedule a DELETE request

First, sign up for an account with EasyCron here. Locate your EasyCron API key in your user dashboard.

Then, insert your EasyCron API key into your code.

Next, we will use the datetime module to generate a cron expression for 24 hours from now (although you can configure the timedelta argument to be whatever you want)

1# In production, you should store your API keys in environment variables
2EASYCRON_API_TOKEN = "YOUR_EASYCRON_API_KEY"
3
4from datetime import datetime, timedelta, timezone
5
6# Calculate the time 24 hours from now in EasyCron's expected format
7# EasyCron uses a slightly different format, but for simplicity, we'll use the standard UTC format
8# and convert this into a cron expression
9time_24_hours_from_now = datetime.now(timezone.utc) + timedelta(hours=24)
10cron_minute = time_24_hours_from_now.minute
11cron_hour = time_24_hours_from_now.hour
12cron_day = time_24_hours_from_now.day
13cron_month = time_24_hours_from_now.month
14cron_year = time_24_hours_from_now.year
15
16cron_expression = f'{cron_minute} {cron_hour} {cron_day} {cron_month} * {cron_year}'

Now, we will schedule a cron job 24 hours from now with EasyCron to send a delete request to AssemblyAI for the transcript that we just generated.

1import requests
2
3# EasyCron API endpoint for creating a new cron job
4api_endpoint = 'https://www.easycron.com/rest/add'
5
6# The data to be sent to EasyCron's API
7data = {
8 'token': EASYCRON_API_TOKEN,
9 'url': url_to_call,
10 'cron_expression': cron_expression,
11 'http_method': "DELETE",
12 'http_headers': f'Authorization: {AAI_API_TOKEN}',
13 'timezone': 'UTC'
14}
15
16# Make the request to EasyCron's API
17response = requests.post(api_endpoint, data=data)
18
19# Print the response from EasyCron's API
20print(response.text)

Troubleshooting:

Error 3: The cron expression you entered is invalid or it cannot be matched in a realitic future.: Check that your EasyCron account settings has UTC configured as the default timezone.

Other resources

AssemblyAI DELETE endpoint API reference
EasyCron API reference