Get Started with Rev AI API Webhooks
Tags: api, webhook, speechrecognition
Published: 2022-04-08 15:37:10

By Vikram Vaswani - Developer Advocate

_This tutorial was originally published at [https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/](https://docs.rev.ai/resources/tutorials/get-started-api-webhooks/) on Mar 23, 2022._

# Introduction

If you've used GitHub, PayPal, Slack or any number of other online services, you've already heard of (and maybe even used) webhooks. Webhooks provide an easy way for one application to notify another about a specific event - for example, a Git commit, a PayPal order or a Slack message - so that the notified application can take further action as needed.

Webhooks are a key component of building event-driven Web applications, and they're also well-supported in the various Rev AI ASR APIs. They provide a simple and efficient approach to programmatically managing job requests submitted through the Rev AI APIs.

Here's how it works:

1. An application submits a job to a Rev AI API for processing, supplying a webhook URL in the `callback_url` parameter.
2. Once job processing is complete, the API sends an HTTP POST message to the webhook URL containing the results of processing.
3. The webhook URL handler receives the HTTP POST request, parses the request body and implements further application-specific business logic as needed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwgpigb94nrj4e6tdiix.png)

When developing event-driven ASR applications, it's important to be able to easily test and inspect the data structures received from the Rev AI APIs. A number of freely-available third-party tools and websites are available for webhook testing, including [Webhook.site](https://webhook.site/) and [RequestBin](https://requestbin.com/). This tutorial uses [Webhook.site](https://webhook.site/), but the procedure is broadly similar for other alternatives as well. 

# Assumptions

This tutorial assumes that:
- You have a Rev AI account and access token. If not, [sign up for a free account](https://www.rev.ai/auth/signup) and [generate an access token](https://docs.rev.ai/get-started/#step-1-get-your-access-token).
- You have an audio file to transcribe. If not, use this [example audio file from Rev AI](https://www.rev.ai/FTC_Sample_1.mp3).

# Step 1: Obtain a webhook URL

The first step is to obtain a unique webhook URL, which you can do by visiting [https://webhook.site/](https://webhook.site/) and copying the URL shown on the page. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sau6jepb0rlvg0w0056t.png)

This webhook URL will remain active and waiting for requests for so long as you remain on the page.

# Step 2: Add the webhook as `callback_url` in your API request

Submit an audio file for transcription to Rev AI using the command below. Replace the `<REVAI_ACCESS_TOKEN>` placeholder with your Rev AI access token and the `<WEBHOOK_URL>` placeholder with the webhook URL obtained in Step 1. If you wish to use a different audio file, additionally replace the value of the `media_url` parameter with the URL to your audio file.

```bash
curl -X POST "https://api.rev.ai/speechtotext/v1/jobs" \
     -H "Authorization: Bearer <REVAI_ACCESS_TOKEN>" \
     -H "Content-Type: application/json" \
     -d '{"media_url":"https://www.rev.ai/FTC_Sample_1.mp3","callback_url":"<WEBHOOK_URL>"}'
```

Your API call will return a response like this:

```bash
{
  "id":"8xBckmk6rAqu",
  "created_on":"2022-03-16T14:26:14.151Z",
  "name":"FTC_Sample_1.mp3",
  "callback_url":"https://webhook.site/84de11c2-e60b-4c15-928d-969cd26ce3cc",
  "media_url":"https://www.rev.ai/FTC_Sample_1.mp3",
  "status":"in_progress",
  "type":"async",
  "language":"en"
}
```

# Step 3: Inspect the API response

For asynchronous transcription, the job's turnaround time is a function of the audio length. Files that are approximately 5 minutes in length will be returned within a few minutes. Longer files can take up to 15 minutes. [Learn more about turnaround time](https://docs.rev.ai/api/asynchronous/#turnaround-time-and-chunking).

Once the job's `status` changes, the Rev AI API will submit an HTTP POST request to the configured webhook URL. Here is an example of the POST message body:

```javascript
{
  "job": {
    "id": "8xBckmk6rAqu",
    "created_on": "2022-03-16T14:26:14.151Z",
    "completed_on": "2022-03-16T14:26:53.601Z",
    "name": "FTC_Sample_1.mp3",
    "callback_url": "https://webhook.site/84de11c2-e60b-4c15-928d-969cd26ce3cc",
    "media_url": "https://www.rev.ai/FTC_Sample_1.mp3",
    "status": "transcribed",
    "duration_seconds": 107,
    "type": "async",
    "language": "en"
  }
}
```

This request to the webhook URL will be visible in the Webhook.site interface, which will auto-refresh to display the complete POST request details as shown below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0fx7uaeqz1k1w8rp9z36.png)

As the example above illustrates, the HTTP POST message includes the job identifier and detailed information on the job status, duration and completion time. This information can be used by the application for further decision-making or action, such as: 
- Recording the job status in a database or message queue
- Invoking behavior (for example, an email or SMS notification) prompting additional user action
- Retrieving the transcript data
- Redirecting the message data to another system for further analysis and processing 

## Next steps

Webhooks provide a way to asynchronously receive notifications once a Rev AI job completes. Although optional, they are recommended in production environments, as they are significantly more efficient than repeatedly polling the API for job status and also relatively easy to understand and work with.

Learn more about Rev AI APIs and their support for webhooks by visiting the following links:

- Tutorial: [Use Webhooks to Trigger Job Email Notifications with Node.js, Sendgrid and Express](https://docs.rev.ai/resources/tutorials/send-email-notifications-webhooks/)
- Documentation: Asynchronous Speech-To-Text API [job submission](https://docs.rev.ai/api/asynchronous/reference/#operation/SubmitTranscriptionJob!ct=application/json&path=callback_url&t=request) and [webhooks](https://docs.rev.ai/api/asynchronous/webhooks/)
- Documentation: Sentiment Analysis API [job submission](https://docs.rev.ai/api/sentiment-analysis/reference/#operation/SubmitSentimentAnalysisJob!path=callback_url&t=request) and [webhooks](https://docs.rev.ai/api/sentiment-analysis/webhooks/)
- Documentation: Topic Extraction API [job submission](https://docs.rev.ai/api/topic-extraction/reference/#operation/SubmitTopicExtractionJob!path=callback_url&t=request) and [webhooks](https://docs.rev.ai/api/topic-extraction/webhooks/)
- Documentation: Custom Vocabulary API [submission](https://docs.rev.ai/api/custom-vocabulary/reference/#operation/SubmitCustomVocabulary!path=callback_url&t=request) and [webhooks](https://docs.rev.ai/api/custom-vocabulary/webhooks/)