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
15transcriber = aai.Transcriber()
16transcript = transcriber.transcribe('https://assembly.ai/sports_injuries.mp3')
17
18# Get the transcript ID
19transcript_id = transcript.id
20
21# Construct the URL for the DELETE request
22url_to_call = f"https://api.assemblyai.com/v2/transcript/{transcript_id}"
23
24# Calculate the time 24 hours from now for the cron expression
25time_24_hours_from_now = datetime.now(timezone.utc) + timedelta(hours=24)
26cron_minute = time_24_hours_from_now.minute
27cron_hour = time_24_hours_from_now.hour
28cron_day = time_24_hours_from_now.day
29cron_month = time_24_hours_from_now.month
30cron_year = time_24_hours_from_now.year
31
32# Create the cron expression
33cron_expression = f'{cron_minute} {cron_hour} {cron_day} {cron_month} * {cron_year}'
34
35# EasyCron API endpoint for creating a new cron job
36api_endpoint = 'https://www.easycron.com/rest/add'
37
38# Data payload for EasyCron API
39data = {
40 'token': EASYCRON_API_TOKEN,
41 'url': url_to_call,
42 'cron_expression': cron_expression,
43 'http_method': "DELETE",
44 'http_headers': f'Authorization: {aai.settings.api_key}',
45 'timezone': 'UTC'
46}
47
48# Make the request to EasyCron's API
49response = requests.post(api_endpoint, data=data)
50
51# Print the response from EasyCron
52print("EasyCron API Response:")
53print(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}"
1transcriber = aai.Transcriber()
2
3# this is just an example file
4transcript = transcriber.transcribe('https://assembly.ai/sports_injuries.mp3')

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