Skip to main content

Documentation Index

Fetch the complete documentation index at: https://assemblyai.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

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

import assemblyai as aai
import requests
from datetime import datetime, timedelta, timezone

# Set up AssemblyAI API key
# In production, store this in an environment variable
aai.settings.api_key = "YOUR_ASSEMBLYAI_API_KEY"

# Get an EasyCron API key here: https://www.easycron.com/user/token
# Set up EasyCron API key
# In production, store this in an environment variable
EASYCRON_API_TOKEN = "YOUR_EASYCRON_API_KEY"

# Create transcriber instance and transcribe audio
config = aai.TranscriptionConfig(speech_models=["universal-3-pro", "universal-2"])
transcriber = aai.Transcriber()
transcript = transcriber.transcribe('https://assembly.ai/sports_injuries.mp3', config)

# Get the transcript ID
transcript_id = transcript.id

# Construct the URL for the DELETE request
url_to_call = f"https://api.assemblyai.com/v2/transcript/{transcript_id}"

# Calculate the time 24 hours from now for the cron expression
time_24_hours_from_now = datetime.now(timezone.utc) + timedelta(hours=24)
cron_minute = time_24_hours_from_now.minute
cron_hour = time_24_hours_from_now.hour
cron_day = time_24_hours_from_now.day
cron_month = time_24_hours_from_now.month
cron_year = time_24_hours_from_now.year

# Create the cron expression
cron_expression = f'{cron_minute} {cron_hour} {cron_day} {cron_month} * {cron_year}'

# EasyCron API endpoint for creating a new cron job
api_endpoint = 'https://www.easycron.com/rest/add'

# Data payload for EasyCron API
data = {
    'token': EASYCRON_API_TOKEN,
    'url': url_to_call,
    'cron_expression': cron_expression,
    'http_method': "DELETE",
    'http_headers': f'Authorization: {aai.settings.api_key}',
    'timezone': 'UTC'
}

# Make the request to EasyCron's API
response = requests.post(api_endpoint, data=data)

# Print the response from EasyCron
print("EasyCron API Response:")
print(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:
import assemblyai as aai

# set the API key
aai.settings.api_key = f"{YOUR_API_KEY}"
config = aai.TranscriptionConfig(speech_models=["universal-3-pro", "universal-2"])
transcriber = aai.Transcriber()

# this is just an example file
transcript = transcriber.transcribe('https://assembly.ai/sports_injuries.mp3', config)
Store the transcript ID
transcript_id = transcript.id
Using this, we now construct the URL that we want our cron job to call.
url_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)
# In production, you should store your API keys in environment variables
EASYCRON_API_TOKEN = "YOUR_EASYCRON_API_KEY"

from datetime import datetime, timedelta, timezone

# Calculate the time 24 hours from now in EasyCron's expected format
# EasyCron uses a slightly different format, but for simplicity, we'll use the standard UTC format
# and convert this into a cron expression
time_24_hours_from_now = datetime.now(timezone.utc) + timedelta(hours=24)
cron_minute = time_24_hours_from_now.minute
cron_hour = time_24_hours_from_now.hour
cron_day = time_24_hours_from_now.day
cron_month = time_24_hours_from_now.month
cron_year = time_24_hours_from_now.year

cron_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.
import requests

# EasyCron API endpoint for creating a new cron job
api_endpoint = 'https://www.easycron.com/rest/add'

# The data to be sent to EasyCron's API
data = {
    'token': EASYCRON_API_TOKEN,
    'url': url_to_call,
    'cron_expression': cron_expression,
    'http_method': "DELETE",
    'http_headers': f'Authorization: {AAI_API_TOKEN}',
    'timezone': 'UTC'
}

# Make the request to EasyCron's API
response = requests.post(api_endpoint, data=data)

# Print the response from EasyCron's API
print(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