Connecting Your App to HeyGen with OAuth 2.0
This guide will walk you through connecting your app to HeyGen using OAuth 2.0. We'll use curl commands to demonstrate each step, making it easy to test the process. HeyGen uses the Authorization Code Flow with PKCE (Proof Key for Code Exchange) for added security.
Prerequisites
Before you begin, ensure you have:
- Your HeyGen Client ID
- Your Redirect URI
(This will be provided by the Partnerships team after you have submitted your [Integration Intake form](https://2ykg0d9nevz.typeform.com/to/m3EYcOaM))
Step 1: Initiate User Authorization
To begin the OAuth process, redirect the user to HeyGen's authorization URL. Create this URL (replace the capitalized parts with your info):
https://app.heygen.com/oauth/authorize?client_id=YOUR_CLIENT_ID&state=RANDOM_STATE&redirect_uri=YOUR_REDIRECT_URI&response_type=code
- YOUR_CLIENT_ID: Client ID. (e.g.,
abc123
) - RANDOM_STATE: A unique string to maintain state. (e.g.,
xyz789
) - YOUR_REDIRECT_URI: Your approved redirect URI, ensure URL-encoded. (e.g.,
https://yourapp.com/oauth/callback
)
Step 2: Handle the Authorization Callback
After approval, you'll be redirected to your Redirect URI with a code
parameter. It'll look like this:
https://yourapp.com/oauth/callback?code=AUTHORIZATION_CODE&state=RANDOM_STATE
Verify that the state matches the one you sent in Step 1, then extract the AUTHORIZATION_CODE
.
Step 3: Exchange Authorization Code for Access Token
Exchange the authorization code for an access token using this curl command:
curl -X POST https://api2.heygen.com/v1/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "code=AUTHORIZATION_CODE" \
-d "client_id=YOUR_CLIENT_ID" \
-d "grant_type=authorization_code" \
-d "redirect_uri=YOUR_REDIRECT_URI" \
-d "code_verifier=YOUR_CODE_VERIFIER"
{
"token_type": "Bearer",
"access_token": "YyzWfeiqmklLsvzNsallQgUgfKHaNSnpv60BNgLGsC",
"expires_in": 864000,
"refresh_token": "dfU22fh6iD4aCkpy9GI32ulJXVe5eC8u4rt4SXedJHHich6L"
}
Save the access_token
and refresh_token
- you'll need them!
Step 4: Use the Access Token
Now you can make requests to HeyGen's API. Here's an example:
curl -X GET https://api.heygen.com/v1/some-endpoint \
-H "Authorization: Bearer NEW_ACCESS_TOKEN"
The HeyGen API endpoints support both authentication methods:
- For OAuth Access Tokens:
- Use the
Authorization
header with the Bearer scheme. - Format:
Authorization: Bearer YOUR_ACCESS_TOKEN
- This is the method enabled by OAuth integration.
- Use the
- For API Tokens:
- Use the
X-API-KEY
header. - Format:
X-API-KEY: YOUR_API_TOKEN
- This is the existing method.
- Use the
Step 5: Refresh the Access Token
Access tokens expire. Keep track of token expiration and refresh as needed. When the access token expires, use the refresh token to obtain a new one:
curl -X POST https://api2.heygen.com/v1/oauth/refresh_token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "client_id=YOUR_CLIENT_ID" \
-d "grant_type=refresh_token" \
-d "refresh_token=REFRESH_TOKEN"
{
"token_type": "Bearer",
"access_token": "YyzWfeiqmklLsvzNsallQgUgfKHaNSnpv60BNgLGsC",
"expires_in": 864000,
"refresh_token": "dfU22fh6iD4aCkpy9GI32ulJXVe5eC8u4rt4SXedJHHich6L"
}
Best Practices and Security Considerations
- Always use HTTPS for all OAuth-related requests.
- Store tokens securely and never expose them client-side.
- Implement token rotation and regularly refresh access tokens. If a request fails, check if the access token has expired.
- Validate the
state
parameter to prevent CSRF attacks. - Use short-lived access tokens and long-lived refresh tokens.
- Implement proper error handling for token expiration and other OAuth-related errors.
Conclusion
This guide provides a comprehensive approach to integrating HeyGen's API using OAuth 2.0 with the Authorization Code Flow and PKCE. By following these steps and best practices, you can securely authenticate users and access HeyGen's API on their behalf.
Updated 2 days ago