Example - Express.js
A example Express.js application for integrating with Guru’s API
Introduction
In this guide, we will walk through the general Guru API workflow: authenticating your requests, uploading a video, and fetching the results. You can clone an example Express.js app that implements this workflow here and read the code walkthrough below:
Requirements
- A Guru Console account to get your API credentials
- A Guru Schema (create your first one in Quickstart.)
Code Walkthrough
Authentication
Authentication with the Guru API occurs using OAuth tokens. You must include your authentication token as a header on each HTTP request you make like this:
Authorization: <token>
Your service will obtain its authentication token using an OAuth Client-Credential Flow:
-
Exchange your client ID and secret with https://customer-console-prod.auth.us-west-2.amazoncognito.com for an access token. The value of the expires_in field in the response is the number of seconds until this token expires.
-
Store the token along with its expiration date in persistent storage so that it can be re-used on each call. It is important to not request new tokens on each call as your application will be rate limited.
-
Before making a call to the Guru API, check if the token is expired and, if so, refresh it.
-
Make the call to the Guru API using the access token.
Here is working code that performs the credential exchange:
Analyzing Videos
Uploading a video for analysis is a three-step process:
-
Call the Create API to specify the video’s metadata. This will tell Guru some basic information about the video such as its size and what
schema
should be applied to the video. This information helps deliver more accurate analysis results. The API will return a URL that specifies where the video should be uploaded to. -
Upload the video content to the URL returned in step 1. The video will be encoded as multipart/form-data in the request.
-
Poll the Analysis API until the video is ready. It will typically take 30 seconds for analysis to complete, though longer wait times may be experienced for larger videos. See below for details on each individual API call.
Here is the code block in our Express.js app that handles uploading a video.
Uploading video
And here is the code block that fetches the video analysis
Fetching Analysis
The general workflow Guru API workflow— authenticating, uploading, and analyzing videos—is now complete. Next steps:
- Check out our API Reference
- Learn how to customize your AI video analysis