Back

Step by Step Guide to Building a Frame.io API Integration in C#

Aug 16, 20247 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Frame.io API integration? You're in for a treat. Frame.io's API is a powerhouse that'll let you seamlessly incorporate their video collaboration tools into your C# projects. Whether you're building a custom workflow or enhancing your existing app, this guide will get you up and running in no time.

Prerequisites

Before we jump in, make sure you've got:

  • A C# development environment (Visual Studio, VS Code, or your preferred IDE)
  • A Frame.io account with API credentials (if you don't have these, hop over to Frame.io and set them up real quick)

Got everything? Great! Let's get coding.

Setting up the project

First things first, let's create a new C# project. Fire up your IDE and start a new console application. We'll keep it simple for now, but feel free to adapt this to your specific project type.

Next, we need to grab some packages. Open up your Package Manager Console and run:

Install-Package Newtonsoft.Json
Install-Package RestSharp

These will make our lives easier when dealing with JSON and HTTP requests.

Authentication

Alright, time to get that access token. Frame.io uses OAuth 2.0, but don't worry, it's not as scary as it sounds.

Here's a quick snippet to get you started:

using RestSharp; using Newtonsoft.Json.Linq; var client = new RestClient("https://api.frame.io/v2/oauth/token"); var request = new RestRequest(Method.POST); request.AddHeader("content-type", "application/x-www-form-urlencoded"); request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET", ParameterType.RequestBody); IRestResponse response = client.Execute(request); var token = JObject.Parse(response.Content)["access_token"].ToString();

Remember to replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your actual credentials. And hey, in a real-world scenario, you'd want to store these securely and implement proper token management.

Basic API requests

Now that we're authenticated, let's make a simple GET request:

var client = new RestClient("https://api.frame.io/v2/me"); var request = new RestRequest(Method.GET); request.AddHeader("Authorization", $"Bearer {token}"); IRestResponse response = client.Execute(request); Console.WriteLine(response.Content);

This will fetch info about the authenticated user. Cool, right?

Implementing key features

Uploading assets

Uploading files is a bit more involved, but here's the gist:

var client = new RestClient("https://api.frame.io/v2/assets"); var request = new RestRequest(Method.POST); request.AddHeader("Authorization", $"Bearer {token}"); request.AddFile("file", "path/to/your/file.mp4"); request.AddParameter("parent_asset_id", "PARENT_ASSET_ID"); IRestResponse response = client.Execute(request);

Managing projects and folders

Creating a project is a breeze:

var client = new RestClient("https://api.frame.io/v2/projects"); var request = new RestRequest(Method.POST); request.AddHeader("Authorization", $"Bearer {token}"); request.AddJsonBody(new { name = "My Awesome Project" }); IRestResponse response = client.Execute(request);

Adding comments and annotations

Let's add a comment to an asset:

var client = new RestClient("https://api.frame.io/v2/comments"); var request = new RestRequest(Method.POST); request.AddHeader("Authorization", $"Bearer {token}"); request.AddJsonBody(new { asset_id = "ASSET_ID", text = "Great work on this scene!" }); IRestResponse response = client.Execute(request);

Grab those review links like this:

var client = new RestClient("https://api.frame.io/v2/assets/ASSET_ID/review_links"); var request = new RestRequest(Method.GET); request.AddHeader("Authorization", $"Bearer {token}"); IRestResponse response = client.Execute(request);

Error handling and best practices

Always implement retry logic for failed requests and respect rate limits. Here's a simple retry example:

int maxRetries = 3; for (int i = 0; i < maxRetries; i++) { try { // Your API call here break; } catch (Exception ex) { if (i == maxRetries - 1) throw; Thread.Sleep(1000 * (i + 1)); // Exponential backoff } }

Testing the integration

Don't forget to write tests! Here's a quick unit test example using NUnit:

[Test] public void TestGetUserInfo() { var api = new FrameIoApi(token); var userInfo = api.GetUserInfo(); Assert.IsNotNull(userInfo); Assert.IsNotEmpty(userInfo.Email); }

Optimizing performance

For better performance, use asynchronous operations:

public async Task<string> GetUserInfoAsync() { var client = new RestClient("https://api.frame.io/v2/me"); var request = new RestRequest(Method.GET); request.AddHeader("Authorization", $"Bearer {token}"); var response = await client.ExecuteAsync(request); return response.Content; }

And consider implementing caching for frequently accessed data.

Conclusion

And there you have it! You're now equipped to build a robust Frame.io API integration in C#. Remember, this is just the tip of the iceberg. Frame.io's API has a ton more features to explore.

Keep experimenting, keep building, and most importantly, have fun with it! If you get stuck, the Frame.io API docs are your best friend. Happy coding!