DEV Community

Cover image for How to trim videos in Node using the Veed API
Philip Obosi
Philip Obosi

Posted on • Updated on

How to trim videos in Node using the Veed API

In this age where media has become mainstream, numerous applications have a growing need to manipulate media files e.g video, audio and images programatically. A common use case is being able to trim portions of videos programatically and get back the clip of the trimmed portion.

In this article, we will learn how to programatically trim videos in Node JS using the VEED API. is a simple but powerful online video editor that allows users edit videos ONLINE and for FREE. VEED makes it easy for you to do things like resize videos, trim videos, crop videos, transcribe and subtitle videos, add filters to videos and lots more. There is also a video editing API that allows you access these functionalities programatically.

Without further ado, let’s get started.

Installation & setup

To get started, we will set up the project, install all necessary dependencies and prepare to get our final result in 4 simple steps.

1. Signup and get your API Key

Follow the link to signup for your free Veed account.

Once you finish signup, you should see your Workspace Dashboard. This is your home for managing your video editing activity on Veed.

On your Workspace Dashboard, click on the Settings tab on the side menu. This will open up a section as shown below. At the bottom of the page, you will find a button that reads “Enable API. Click on that button to enable the API section of your dashboard.

Once, you have done that, you will be taken to the section shown below. Here, you can copy your API keys which will be required for all interactions with the API. You will also notice that an API tab has now been added to your side menu for easy access subsequently.

Make sure to use the test key key_test_XXXXXXXXXXXXXX while in development and switch to the production key key_live_XXXXXXXXXXXXXX for your deployment environment.

2. Install Node and NPM

Make sure you have npm installed on your system:

node -v && npm -v
Enter fullscreen mode Exit fullscreen mode

You should see the respective version numbers printed to the console. If not, you can go to the official Node.js site to get installation instructions for your platform.

Now that you have node and NPM installed, run the commands below to setup the project directory.

3. Setup project directory

Create project directory.

mkdir trim-video-app 
Enter fullscreen mode Exit fullscreen mode

Enter the directory you just created and initalize the project.

cd trim-video-app && npm init -y
Enter fullscreen mode Exit fullscreen mode

This will create a package.json file which holds configurations pertaining to the project but most importantly, it will contain the dependencies for this project.

4. Install dependencies

For this project, the only dependency we need to install is the VEED API Node library. To do so, execute the command below in your terminal.

npm i @veedstudio/veed-node --save
Enter fullscreen mode Exit fullscreen mode

Now that we have successfully installed the necessary dependencies, for the final step we need to setup our webhook.

5. Setting up your webhook

Instead of polling the API each time in order to know when our render is complete, the VEED API provides support for a webhook so that users can receive events from time to time when an action worthy of note occurs e.g a render has started, a render has completed e.t.c

To get a webhook URL for this purpose, head over to to retrieve your webhook URL. Copy your unique URL and head over to your Workspace dashboard. On the API tab, you’d see an input labeled Webhook URL, paste the URL you had copied earlier in there and click “Save”.

Now that we have done all that, we are ready to start trimming videos.

Trimming the video

Now we are going to write the piece of code that does the actual trimming. To get started, open the folder we created earlier in your favourite IDE and create a file called index.js.

Copy and paste the code below into this file.

const veed = require("@veedstudio/veed-node")(API_KEY);

const FILE_URL = ''

const renderOptions = {
    "elements": [
            "type": "video",
            "params": {
                "source": {
                    "url": FILE_URL
                "trim": {
                    "from": 2.0,
                    "to": 5.0

Enter fullscreen mode Exit fullscreen mode

Above, we start by importing the VEED package and initalizing it inline by passing the API_KEY from our dashboard as an argument.

Next we declare a variable called FILE_URL which holds the URL to a sample video to be used for this demo.

After that, we move on to constructing the render object. The render object is a set of instructions in the form of object properties, that describes which edits should be applied to your media asset. It is typically comprised of render elements and render parameters. In the code snippet above, notice that elements is an array, suggesting that you can add as many elements as you want along with their configurations to achieve your desired outcome.

For our use case, we have added one video element. As part of the params, we specify the source of the video which points to our sample video and the trim configuration which specifies what point the trimming starts from and where it ends using the to property.

Finally, we used the VEED package to create a new render while passing the renderOptions to it as an argument.

Now we can run the script from the terminal using the command below.

 node index.js
Enter fullscreen mode Exit fullscreen mode

On success, you should find a payload containing the render ID as shown below printed on the console.

{ id: '163688fb-4626-4f12-a165-12576fe54023' }
Enter fullscreen mode Exit fullscreen mode

Getting the trimmed video

There are several ways to get the trimmed video.

1. Using the webhook

Return to the page where you had copied the webhook URL that you added to the dashboard. You will notice that the several events are being sent to this URL as the render proceeds.

You can find the list of events received on the left. You can click on any them to see the data that was sent in each payload. You would notice the the render starts out from progress percentage 0, up until 100 when the render is completed.

  "type": "RENDER/PROGRESS",
  "payload": {
    "render_id": "17cf9aad-807d-4f68-98b7-f748b8d67c22",
    "percentage": 0
Enter fullscreen mode Exit fullscreen mode

On completion, you get a RENDER/SUCCESS event which contains the URL to the trimmed video as requested.

  "type": "RENDER/SUCCESS",
  "payload": {
    "render_id": "e52aad52-5ab0-465d-8341-fdabd6302cf1",
    "progress": 100,
    "url": ""
Enter fullscreen mode Exit fullscreen mode

2. Poll the API

Without the webhook, there is no way to tell at what exact time a render is complete. Hence, another approach will be to poll the API. You can use the VEED package for this. Simply replace veed.render.create(renderOptions) with veed.render.get({id:'YOUR_RENDER_ID_GOES_HERE'}). Run the script again. You should see the response printed to the console. If the render is completed and the download URL is ready, you will get a response like the one below:

  id: '540ba741-bead-42cd-bcbe-dcd2d0663d8d',
  workspace_id: '78c897c9-810d-4a33-97e2-15c796a05161',
  data: { elements: [ [Object], [Object] ] },
  latest_event: {
    type: 'RENDER/SUCCESS',
    payload: {
      render_id: '540ba741-bead-42cd-bcbe-dcd2d0663d8d',
      progress: 100,
      url: ''
  created_at: 1614588003775,
  modified_at: 1614588032628
Enter fullscreen mode Exit fullscreen mode

You can now copy the url and paste it into your browser. Hit enter and the download should start automatically.


We have successfully learnt how to trim videos in Node using the VEED API. While this is merely a demo showcasing what’s possible, feel free to take things further by building out your own application or API that utilizes this API in a more clever way and share in the comment section below.

I’d love to see the magic you create. See you in the next one!


Top comments (0)