Pular para conteúdo

JavaScript SDK

Shutterstock provides a JavaScript SDK to make it easier to use the API from JavaScript or Node.JS applications. The SDK includes methods for all endpoints in the API, so you can do anything with the SDK that you can do with the API by itself. The SDK has the same prerequisites and uses the same authentication as the API, so you can use the same application and authentication methods that you use with the API.

Using the SDK has advantages over accessing the API directly:

  • It's more readable than direct REST calls to the API because the SDK provides methods like searchImages() and licenseVideos() to use instead of URLs to the API.
  • Using the SDK makes it easier to integrate the Shutterstock API with your code because you don't have to use a separate HTTP request library and spend time integrating other packages.
  • Using the SDK keeps your code up to date with the API's latest features and ensures that your code is accessing the API correctly.

For complete documentation about the SDK, including installation instructions, see the NPM package or the GitHub repository.

Get JavaScript SDK

Installation

The easiest way to install the SDK is to use NPM or Yarn:

npm install shutterstock-api --save
yarn add shutterstock-api

Authentication

As with the API, to use the SDK you must create an application at https://www.shutterstock.com/account/developers/apps and note the client ID and client secret. Then, you can use that client ID and client secret for basic authentication or to request a token for OAuth authentication. For information about authenticating with the SDK, see the GitHub repository.

Using the SDK

After you have installed the SDK and authenticated to the API, you can use its classes and methods to access the Shutterstock public API. For example, to search for images with OAuth authentication, pass your token to the SDK, create a shutterstock-api.ImagesApi object, and pass your search parameters to its searchImages() method. The API returns the results in a JavaScript object that has the same information as the GET /v2/images/search endpoint API response.

const sstk = require('shutterstock-api');

sstk.setAccessToken(process.env.SHUTTERSTOCK_API_TOKEN);

const api = new sstk.ImagesApi();

const queryParams = {
  query: 'New York',
  sort: 'popular',
  orientation: 'horizontal'
};

api.searchImages(queryParams)
.then(function({data}) {
  console.log(data);
})
.catch(function(error) {
  console.error(error);
});

If your account has full API access, you can request licenses for images and download them, as in this example:

const sstk = require('shutterstock-api');

sstk.setAccessToken(process.env.SHUTTERSTOCK_API_TOKEN);

const api = new sstk.ImagesApi();

const body = new sstk.LicenseImageRequest([{
  image_id: '2085276112',
  subscription_id: process.env.SUBSCRIPTION_ID,
}]);

api.licenseImages(body)
.then(function({data}) {
  console.log(data);
})
.catch(function(error) {
  console.error(error);
});

As a shortcut to creating the LicenseImageRequest in the previous example, you can also look up the JSON body that the SDK sends to the API and pass that raw JSON to the SDK instead of the objects. You can find the JSON schemas in the API reference. For example, this example adds images to a collection (lightbox) using the SDK objects:

const sstk = require('shutterstock-api');

sstk.setAccessToken(process.env.SHUTTERSTOCK_API_TOKEN);

const api = new sstk.ImagesApi();

const image1 = new sstk.CollectionItem("1320889664");
const image2 = new sstk.CollectionItem("495863218");

const imagesToAddToCollection = new sstk.CollectionItemRequest([image1, image2]);

const collectionId = '64706280';

api.addImageCollectionItems(collectionId, imagesToAddToCollection)
.then(function() {
  console.log('Success!');
})
.catch(function(error) {
  console.error(error);
});

Depending on how you've set up your application, it might be simpler to send the JSON directly, as in this example:

const sstk = require('shutterstock-api');

sstk.setAccessToken(process.env.SHUTTERSTOCK_API_TOKEN);

const api = new sstk.ImagesApi();

const collectionId = '64706280';
const imagesToAddToCollection = {
  items: [
    {id: '1320889664', media_type: 'image'},
    {id: '495863218', media_type: 'image'}
  ]
};

api.addImageCollectionItems(collectionId, imagesToAddToCollection)
.then(function() {
  console.log('Success!');
})
.catch(function(error) {
  console.error(error);
});

For more examples, see the documentation in the GitHub repository and the examples in the API reference.

Chegamos a mais de 475 milhões de recursos na Shutterstock.com em 30 de novembro de 2023.

© 2003-2024 Shutterstock, Inc.