Tutorials · 4 min read

birds migrating

Migrate your Node app to our new API client

Our migration guide from the NodeJS SDK to the NODEJS API client makes migrating your app easy. See how I did 2 migrations with just a few minor changes to my code.

Doug Sillars

June 4, 2021

We recently launched a new TypeScript NodeJS API client that replaces the NodeJS SDK. The NodeJS SDK is not going anywhere - so you can keep using it - but no new features will be added to the SDK. All new features will now be added to the Client API.

Now, as we add new features, you might want to reduce the number of dependencies in your codebase and remove the NodeJS SDK - migrating to the new API Client. We've prepared a migration guide to help you along the path.

Share a video migration

The changes are quite simple. Let's walk through how I migrated the share.a.video application to the apiclient:

The share.a.video application uses a livestream to share a recorded video. This lets all the viewers watch the video at the same time.

In this application, the Node backend does a search of the livestreams in my account. First I added the SDK, and then I did a search for all livestreams:

//apivideo
const apiVideo = require('@api.video/nodejs-sdk');

<snip>

	//get data on both movies:
	const client = new apiVideo.Client({ apiKey: apiVideoKey });
	let allLiveStreams = client.lives.search();

To migrate this to the new API client, I only have to change these three lines:

  • Update the library I am going to use
  • change the apiVideo.Client to apiVideoClient, and place the parameters in brackets
  • lives to liveStreams
  • search() to list()
const apiVideoClient = require('@api.video/nodejs-client');

<snip>

	//get data on both movies:
	const client = new apiVideoClient({ apiKey: apiVideoKey });
	let allLiveStreams = client.liveStreams.list();

That's it. The code now runs on the new API client!

resume a video

resume.a.video finds a user's last session with a video, and then begins playback at the last second viewed by that user. (just like when you restart a Netflix or YouTube video, it knows where you left off).

In resume.a.video the Node SDK was not querying analytics or sessions properly, so I used raw API calls:

  • Authenticate my apiToken
  • Receive all sessions for the user
  • get the latest session data for the user for the most recent time,

With the Node API Client, this is now all possible with the client, and no more raw API calls.

When the user submits their username and the video they'd like to watch, we provide the Node server with the videoId and userName. We make a request to the rawStatistics.listVideoSessions endpoint, asking for just one session.

If there are sessions, we grab the number of sessions, but if there are no sessions for that user, we know we can commence the video playback at time=0.

//first we'll get the session data for the video and the metadata userName
	var params = { videoId, metadata, currentPage, pageSize};
	console.log("params",params)
	const client = new apiVideoClient({apiKey: apiVideoKey});
	const result = client.rawStatistics.listVideoSessions(params);
	
	result.then(function(videos) {
	//	console.log("Allsessions that match", videos);
		const numberSessions = videos.pagination.itemsTotal;
		console.log("number of results: ", numberSessions);

		if(numberSessions >0){
			<we'll get the last session>
		}else{
			var videoUrl = 'https://embed.api.video/vod/'+videoId+'?metadata[userName]='+userName+'#autoplay';
						return res.render('video', {videoUrl});

		}	

The user has sessions with the video

In the case that the user has watched this video, we'll be in the if(numberSessions >0) conditional. We want to know how many sessions the user has had and collect the last one (which will have the last watched moment of the video).

So, we make a second request to the same endpoint to get the most recent session for this user and this video and collect the sessionId.

if(numberSessions >0){
			currentPage = numberSessions;
			const lastSessionParams = { videoId, metadata, currentPage, pageSize};
			console.log("lastSessionParams", lastSessionParams);
			const lastSessionResult = client.rawStatistics.listVideoSessions(lastSessionParams);
			lastSessionResult.then(function(lastSession) {
				console.log("lastSesson: ", lastSession);
				//this is the last session
				var sessionId =  lastSession.data[0].session.sessionId;

With the sessionId, we can then call the client.rawStatistics.listSessionEvents(sessionParams). Each event has the time recorded in the video when the event happened. We retrieve this time value (in seconds) and append that to the video url with the #t url attribute.

				sessionData.then( function(lastSessionData) {
					//this is the session data
					console.log("session events", lastSessionData);
					var numberOfEvents = lastSessionData.pagination.itemsTotal;
					console.log("sessionevents", numberOfEvents);
					var lastTimeRecorded  = lastSessionData.data[numberOfEvents -1].at;
					console.log("lastTimeRecorded", lastTimeRecorded);

					var videoUrl = 'https://embed.api.video/vod/'+videoId+'?metadata[userName]='+userName+'#autoplay;t='+lastTimeRecorded;
						return res.render('video', {videoUrl});


				});

This tells the player to begin video playback at the time the last session ended, completing the task that resume.a.video was built to do. With many fewer lines of code, the new NODE api client has simplified the application, and it runs faster.

Migrate your app

Our new Node API client is where all new api.video features will appear, as we have deprecated the Node SDK. As this post shows, the migration from the older SDK to the API client is easy and relatively painless, so you can keep your application on our latest code base with minimal refactoring.

Try out more than 80 features for free

Access all the features for as long as you need.
No commitment or credit card required

Video API, simplified

Fully customizable API to manage everything video. From encoding to delivery, in minutes.

Built for Speed

The fastest video encoding platform. Serve your users globally with 140+ points of presence. 

Let end-users upload videos

Finally, an API that allows your end-users to upload videos and start live streams in a few clicks.

Affordable

Volume discounts and usage-based pricing to ensure you don’t exceed your budget.