Sirv REST API – Node.js examples

On this page

Use the Sirv REST API to perform over 40 common tasks on your Sirv account, such as uploading, searching, converting videos to spins, adding meta, fetching a remote image by URL and more.

Visit the REST API reference for a list of all methods.

Connect to Sirv with Node.js

First, you should create an API client in your Sirv account. You'll be given a client ID and client Secret.

Use your client ID and secret in the script below to request a token:

'use strict';

const http = require('https');

const options = {
  'method': 'POST',
  'hostname': 'api.sirv.com',
  'path': '/v2/token',
  'headers': {
  'content-type': 'application/json'
  }
};

const clientId = 'ENTER_CLIENT_ID_HERE';
const clientSecret = 'ENTER_CLIENT_SECRET_HERE';

const req = http.request(options, (res) => {
  const chunks = [];

  res.on('data', (chunk) => {
  chunks.push(chunk);
  });

  res.on('end', () => {
  const body = Buffer.concat(chunks);
  const apiResponse = JSON.parse(body.toString());

  console.log('token:', apiResponse.token);
  console.log('expiresIn:', apiResponse.expiresIn);
  console.log('scope:', apiResponse.scope);
  });
});

req.write(JSON.stringify({
  clientId,
  clientSecret
}));

req.end();

Once you have a token, you can perform any of the 40+ REST API methods.

Example scripts

The sample scripts below will show you some typical uses of the REST API.

Upload file

Upload a file to your Sirv account.

Example upload script:

var request = require("request");
var fs = require("fs");

fs.readFile('/path/to/local-file.jpg', (err, data) => {
 if (err) throw err;

 var options = {
  method: 'POST',
  url: 'https://api.sirv.com/v2/files/upload',
  qs: {filename: '/path/to/uploaded-image.jpg'},
  headers: {
   'content-type': 'image/jpeg',
   authorization: 'Bearer eyJhb...BZCSg'
  },
  body: data
 };

 request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
 });

});

Search

Node.js example search script:

var http = require("https");

var options = {
 "method": "POST",
 "hostname": "api.sirv.com",
 "port": null,
 "path": "/v2/files/search",
 "headers": {
  "content-type": "application/json",
  "content-length": "30",
  "authorization": "Bearer RECEIVED_AUTH_TOKEN_HERE"
 }
};

var req = http.request(options, function (res) {
 var chunks = [];

 res.on("data", function (chunk) {
  chunks.push(chunk);
 });

 res.on("end", function () {
  var body = Buffer.concat(chunks);
  console.log(body.toString());
 });
});

req.write(JSON.stringify({ query: 'basename:*.spin' }));
req.end();

Support

If you need help connecting with Node.js or using any of the Sirv REST API methods, please contact the Sirv support team.

Was this article helpful?

Get help from a Sirv expert