Hopp til innholdet

Shutterstock API tutorial: Shutterstock CLI

This version of the tutorial walks you through the steps using the Shutterstock command-line client (CLI). For other API clients, see the tutorial overview.

Part 1: Setting up a Shutterstock API account

Instead of using your shutterstock.com username and password, the API uses an access token that is tied to your account and to an application. This application represents the application or program that you're using to access the API. That application can be a big, complicated program, or some simple commands like the examples in this tutorial.

Here's how to get an application and a token:

  1. Create an account at https://www.shutterstock.com if you don't already have one.

  2. Create an application at https://www.shutterstock.com/account/developers/apps with these settings:

    • App name: Specify a name for your application, such as "Shutterstock API tutorial app"
    • Callback URL: Specify localhost
    • Referrer: Leave this field blank
    • Included products: Accept the default values; these products refer to the free image subscription and other access that you application has by default, including basic use of reverse image search and custom music generation
    • Subscriptions: Select the check box labeled Start free image subscription
    • Company name: Specify a name for your company or organization or your own name
    • Website: Specify the name of a web site for you, your company, or your organization, such as example.com
    • Intended use: Select Personal, academic, or hackathon project
    • Description: Specify a description for the application, such as "Trying the API tutorial"
  3. Read and agree to the Shutterstock terms of service. The application settings look like this:

  4. Click Save. The new app appears on your Developers page. It looks like this:

    Later, you can create other apps to keep different API projects separate.

  5. On the application page, click Generate token.

  6. On the Select Scopes page, select the check box next to every scope. These scopes are permissions that control what you can do with the token. In this case, you need the token to be able to license images, and you can also enable the Collections scope to work with your custom collections.

  7. Click Continue.

  8. In the popup window, sign in to your Shutterstock account and click Allow to allow applications with the token to access your account.

  9. On the Token generated! page, copy the token and save it somewhere on your computer.

    Note: This token is shown only once, so make sure to save a copy before closing the page. Keep the token private, because other people could use it to access your account. The token is valid until you change your Shutterstock account password.

Now that you have an application and a token, you can set up an API client to make API requests on your behalf.

Part 2: Setting up the CLI

The Shutterstock CLI provides commands that you run on the command-line terminal on your computer. Each command corresponds to an endpoint in the API.

To use set up the CLI for use in the tutorial, follow these steps:

  1. Install Python version 3 from python.org.

  2. Open a command-line terminal. See the documentation for your operating system for how to open and use the terminal.

  3. Verify that Python version 3 is installed by running this command:

    python3 --version

    If Python 3 is installed correctly, the terminal prints the version of Python, such as Python 3.9.6.

  4. Verify that the pip package manager is installed by running this command:

    pip --version

    If pip is installed correctly, the terminal prints the version of pip and the related version of Python, such as pip 21.2.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9). Most installations of Python 3 include pip.

  5. If your installation does not have pip, use the package manager for your operating system to install it or see the pip documentation.

  6. Run this command to install the Shutterstock CLI:

    pip install shutterstock-cli
  7. Verify that the CLI is installed by running this command:

    shutterstock --help

    If the CLI is installed correctly, the terminal prints information about the CLI, as in this picture:

  8. Put your token in the SHUTTERSTOCK_API_TOKEN environment variable.

    The way that you save the token in the environment variable is different based on your operating system. On MacOS and Linux, use the export terminal command, as in this example:

    export SHUTTERSTOCK_API_TOKEN=v2/ODYwYmRlNzBiYjMzNTE2M2UyZTQvMTc4NzI2OTM4L2N1c3RvbWVyLzIvWEtXR01HQ1FaVHRLOG85a

    On Windows, use the set terminal command, as in this example:

    set SHUTTERSTOCK_API_TOKEN=v2/ODYwYmRlNzBiYjMzNTE2M2UyZTQvMTc4NzI2OTM4L2N1c3RvbWVyLzIvWEtXR01HQ1FaVHRLOG85a

    When you set an environment variable like this, it persists until you close the terminal window. To make the token available long-term, you may want to configure your terminal to set this variable every time you open a terminal window, such as in your .bashrc or .zshrc file. See the documentation for your terminal or operating system for more information about configuring your shell.

  9. Verify that the CLI works and can use the token by running a command that gets information about your account:

    shutterstock user get-user

    If the CLI can use the token to authenticate to the API, the CLI prints information about your user account to the terminal window. The output looks like this:

    {
      "username": "jsmith",
      "full_name": "John Smith",
      "first_name": "John",
      "last_name": "Smith",
      "language": "en",
      "email": "jsmith@example.com",
      "id": "123456789",
      "customer_id": "123456789",
      "is_premier": false,
      "only_sensitive_use": false,
      "only_enhanced_license": false
    }

Now the CLI is installed and you can use it to access the API.

To get help with the CLI or any command or group of commands, add --help at the end of the command. For example, to get basic information about the CLI and a list of command groups, run shutterstock --help.

Then you can get a list of commands in each group by running the command shutterstock [group name] --help. For example, to get a list of commands that work with images, run shutterstock images --help. These CLI command groups match the groups in the API reference, which contains an example for each command and information about each parameter.

Then you can get information about an individual command by running the command shutterstock [group name] [command name] --help. For example, to get information about the image search command, run shutterstock images search-images --help. The CLI prints information about the command and its parameters.

Part 3: Searching for images

If you're familiar with searching for images on shutterstock.com, searching with the REST API is similar. The shutterstock images search-images command, which uses the GET /v2/images/search endpoint, is the main way to find images based on keywords, categories, and sizes.

  1. Search for one or more images with the shutterstock images search-images endpoint. Here's an example that searches for photos with kites in them:

    shutterstock images search-images --query kites --image-type photo --page 1 --per-page 5 --sort popular --view minimal

    This example search uses the keyword "kites" and selects the first 5 results, sorted by popularity. You can change the parameters to adjust your search; to get a list of parameters, run shutterstock images search-images --help. For more information about the search parameters, see the GET /v2/images/search endpoint in the API reference.

    The response from the API includes info about the search results, including links to thumbnail images and the dimensions of the full images. Most importantly, the results include the IDs of the images, which you'll need to download full images.

    Here's an abbreviated example of what the search results look like:

    {
      "page": 1,
      "per_page": 5,
      "total_count": 116,
      "search_id": "edc213ae-d968-47e5-8cb0-9b16f3a7fbae",
      "data": [
        {
          "id": "1851475780",
          "aspect": 1.5,
          "assets": {
            "small_thumb": {
              "height": 67,
              "url": "https://thumb1.shutterstock.com/thumb_small/250738318/1851475780/stock-photo-wide-shot-of-a-multi-generation-family-walking-with-a-kite-on-a-sunny-beach-with-a-yellow-van-in-1851475780.jpg",
              "width": 100
            },
            "large_thumb": {
              "height": 100,
              "url": "https://thumb1.shutterstock.com/thumb_large/250738318/1851475780/stock-photo-wide-shot-of-a-multi-generation-family-walking-with-a-kite-on-a-sunny-beach-with-a-yellow-van-in-1851475780.jpg",
              "width": 150
            },
            "preview_1000": {
              "url": "https://ak.picdn.net/shutterstock/photos/1851475780/watermark_1000/bb93b8277668a1c8c1e612bc3925e2b8/preview_1000-1851475780.jpg",
              "width": 1000,
              "height": 667
            }
          },
          "contributor": {
            "id": "250738318"
          },
          "description": "Wide shot of a multi-generation family walking with a kite on a sunny beach with a yellow van in background.",
          "image_type": "photo",
          "has_model_release": true,
          "media_type": "image"
        },
        {
          "id": "1851471952",
          "aspect": 1.5017,
          "assets": {
            "small_thumb": {
              "height": 67,
              "url": "https://thumb9.shutterstock.com/thumb_small/250738318/1851471952/stock-photo-full-shot-of-a-multi-generation-family-running-with-a-kite-on-a-sunny-beach-with-the-sea-in-1851471952.jpg",
              "width": 100
            },
            "large_thumb": {
              "height": 100,
              "url": "https://thumb9.shutterstock.com/thumb_large/250738318/1851471952/stock-photo-full-shot-of-a-multi-generation-family-running-with-a-kite-on-a-sunny-beach-with-the-sea-in-1851471952.jpg",
              "width": 150
            },
            "preview_1000": {
              "url": "https://ak.picdn.net/shutterstock/photos/1851471952/watermark_1000/b8aa144b31c26f4690aa16157ae6c65a/preview_1000-1851471952.jpg",
              "width": 1000,
              "height": 666
            }
          },
          "contributor": {
            "id": "250738318"
          },
          "description": "Full shot of a multi-generation family running with a kite on a sunny beach with the sea in background.",
          "image_type": "photo",
          "has_model_release": true,
          "media_type": "image"
        }
      ],
      "spellcheck_info": {}
    }

    Most programming languages have libraries for working with JSON code like this and putting it into usable objects. You can also use command-line programs such as jq to manipulate the data. From there, you can grab the URLs and use the preview images or thumbnails.

    This example sends the response from the API to a jq command that prints the description for each image with a link to the preview image:

    shutterstock images search-images --query kites --image-type photo --page 1 --per-page 5 --sort popular --view minimal | jq -r '.data[] | .description, .assets.preview.url'

    Here's a thumbnail image from the results:

  2. Select an image that you want to try licensing with your account and note its ID number. Your application and token are using the free image subscription, so the results include only images in Shutterstock's free images collection. Therefore, you can use the free subscription to license and download any image in these search results.

  3. Get information about the image, including the sizes that the image is available in, with the shutterstock images get-image {id} command. For example, the image with the thumbnail in the previous step has the ID 1851471952. To get full information about the image, run this command:

    shutterstock images get-image 1851471952

    The full details contain links to thumbnails and previews of multiple sizes, information about the models in the image, and the dimensions and DPI of the different sizes that the image is available in. You'll need those sizes later to make sure that you download the size of the image that you want. In this case, the image is availale in the sizes small (500x333px), medium (1000x666), and huge (5172x3444), denoted by the small_jpg, medium_jpg, and huge_jpg fields in the response. Different images are available in different sizes and formats, but all images are available in at least small and medium JPGs.

    Here are some abbreviated details about the image:

    {
      "id": "1851471952",
      "added_date": "2020-11-10",
      "aspect": 1.5017,
      "assets": {
        "medium_jpg": {
          "display_name": "Med",
          "dpi": 300,
          "file_size": 732305,
          "format": "jpg",
          "height": 666,
          "is_licensable": true,
          "width": 1000
        },
        "small_jpg": {
          "display_name": "Small",
          "dpi": 300,
          "file_size": 204811,
          "format": "jpg",
          "height": 333,
          "is_licensable": true,
          "width": 500
        },
        "huge_jpg": {
          "display_name": "Huge",
          "dpi": 300,
          "file_size": 13290693,
          "format": "jpg",
          "height": 3444,
          "is_licensable": true,
          "width": 5172
        },
        "preview": {
          "height": 299,
          "url": "https://image.shutterstock.com/display_pic_with_logo/250738318/1851471952/stock-photo-full-shot-of-a-multi-generation-family-running-with-a-kite-on-a-sunny-beach-with-the-sea-in-1851471952.jpg",
          "width": 450
        },
        "small_thumb": {
          "height": 67,
          "url": "https://thumb9.shutterstock.com/thumb_small/250738318/1851471952/stock-photo-full-shot-of-a-multi-generation-family-running-with-a-kite-on-a-sunny-beach-with-the-sea-in-1851471952.jpg",
          "width": 100
        },
        "large_thumb": {
          "height": 100,
          "url": "https://thumb9.shutterstock.com/thumb_large/250738318/1851471952/stock-photo-full-shot-of-a-multi-generation-family-running-with-a-kite-on-a-sunny-beach-with-the-sea-in-1851471952.jpg",
          "width": 150
        },
        "huge_thumb": {
          "height": 260,
          "url": "https://image.shutterstock.com/image-photo/full-shot-multigeneration-family-running-260nw-1851471952.jpg",
          "width": 391
        },
        "preview_1000": {
          "url": "https://ak.picdn.net/shutterstock/photos/1851471952/watermark_1000/b8aa144b31c26f4690aa16157ae6c65a/preview_1000-1851471952.jpg",
          "width": 1000,
          "height": 666
        },
        "preview_1500": {
          "url": "https://image.shutterstock.com/z/stock-photo-full-shot-of-a-multi-generation-family-running-with-a-kite-on-a-sunny-beach-with-the-sea-in-1851471952.jpg",
          "width": 1500,
          "height": 999
        }
      },
      "categories": [
        {
          "id": "13",
          "name": "People"
        }
      ],
      "contributor": {
        "id": "250738318"
      },
      "description": "Full shot of a multi-generation family running with a kite on a sunny beach with the sea in background.",
      "image_type": "photo",
      "is_adult": false,
      "is_editorial": false,
      "is_illustration": false,
      "has_model_release": true,
      "has_property_release": false,
      "keywords": [
      ],
      "media_type": "image",
      "model_releases": [
        {}
      ],
      "models": [
        {}
      ],
      "original_filename": "38273.jpg"
    }

That's all you have to do to search for images with the API. The reference for the GET /v2/images/search endpoint has many more fields to search on, and other commands like shutterstock images get-similar-images and shutterstock images get-image-recommendations get images in different ways. For full details, see the images resource in the API reference.

Note that you may see different results when you search through the API from when you search on shutterstock.com. Different subscriptions provide access to different Shutterstock media libraries. API requests use only the media in your subscription's library, including search, details, and licensing requests. For example, when you search with the free subscription, the results are limited to images in the Free stock photos collection.

For this reason, you might see different media by searching on the API versus searching on shutterstock.com. Before trying to license media, make sure that you can access it through the API with your API subscription.

Part 4: Licensing and downloading images

With the free API subscription, you can license and download images from Shutterstock's free collection.

Even though the images don't cost anything to license, your use of the images is legally bound to the conditions of the license that Shutterstock provides. In this case, your API subscription provides a Shutterstock API Platform License.

This license allows you to use the images in your application, but you can't provide or resell the images to other people. Other restrictions apply. For more information about this license, see What is the Shutterstock API Platform License?.

Follow these steps to license and download an image:

  1. Get your subscription ID. If you are using the free subscription, you can find the subscription ID on the https://www.shutterstock.com/account/developers/apps page in your application information, on the Details tab, as shown in the following image.

    Take note of the subscription ID, because you will need it soon.

    To get your subscription ID through the CLI, run this command:

    shutterstock user get-user-subscription-list

    The response includes your subscription ID and the sizes of media that you can license with that subscription. In this example response, the subscription ID is s12345678.

    {
      "data": [
        {
          "id": "s12345678",
          "expiration_time": "2022-06-23T15:08:28-04:00",
          "license": "integrated_media",
          "description": "PS Core Free Product - Image",
          "asset_type": "images",
          "allotment": {
            "start_time": "2021-06-23T15:08:29-04:00",
            "end_time": "2021-07-23T15:08:28-04:00",
            "downloads_left": 500,
            "downloads_limit": 500
          },
          "formats": [
            {
              "media_type": "image",
              "description": "Small",
              "format": "jpg",
              "min_resolution": 500,
              "size": "small"
            },
            {
              "media_type": "image",
              "description": "Med",
              "format": "jpg",
              "min_resolution": 1000,
              "size": "medium"
            },
            {
              "media_type": "image",
              "description": "Huge",
              "format": "jpg",
              "min_resolution": 4000000,
              "size": "huge"
            },
            {
              "media_type": "image",
              "description": "Vector",
              "format": "eps",
              "size": "vector"
            }
          ]
        }
      ]
    }
  2. Put the subscription ID, image ID, and size of the image that you want to license in a JSON file or string.

    Because the image licensing endpoint is a POST request, you must pass the information in the body of the request in JSON format. To store the JSON data in a file, you can open any text or code editor and save it as a file or use the > symbol to write it to a file from the command line.

    To see the format for the JSON data, see the API reference for the command. In this case, we're using the command to license an image: POST /v2/images/licenses. Based on the example of this command and its LicenseImageRequest schema, the JSON data to license image 1851471952 in the medium size looks like this:

    {
      "images": [
        {
          "image_id": "1851471952",
          "subscription_id": "s12345678",
          "size": "medium"
        }
      ]
    }

    To write this data to a file named data.json, run this command:

    echo '{
      "images": [
        {
          "image_id": "1851471952",
          "subscription_id": "s12345678",
          "size": "medium"
        }
      ]
    }' > data.json

    Licensing requests in more complicated scenarios, such as with reseller and enterprise accounts, require more information in the JSON data. For more information about licensing requests in different scenarios, see Licensing and downloading in the API reference.

  3. Send the licensing request to the shutterstock images license-images command and include the JSON data from the previous step as the request body:

    echo '{
      "images": [
        {
          "image_id": "1851471952",
          "subscription_id": "s12345678",
          "size": "medium"
        }
      ]
    }' > data.json
    
    shutterstock images license-images data.json

    If the licensing request is successful, the response from the API includes the URL to the full image. Download the image now, because not all subscription types allow you to download the image again later. Here's an example licensing response:

    {
     "data": [
       {
         "download": {
             "url": "https://download.shutterstock.com/gatekeeper/W3siZCI6ICJzaHV0dGVyc3RvY2stbWVkaWEiLCAiayI6ICJwaG90by8xODUxNDcxOTUyL21lZGl1bS5qcGciLCAiZGMiOiAiaWRsXzEyMyIsICJlIjogMTYyNTA5NjUxMCwgIm0iOiAxfSwgInMxdUVXeGVHNUd2RkRrTndZWmEwN2gycWhEOCJd/shutterstock_1851471952.jpg"
         },
         "allotment_charge": 1,
         "image_id": "1851471952"
       }
     ]
    }

    Now the image is available for your use, according to the terms of the license. Some subscription types allow you to download the image again later by using the shutterstock images download-image endpoint.

    If your licensing request fails, make sure that the JSON data matches the example in the last step. Your JSON must be properly formatted, so make sure that all the quotation marks, braces ({}), and brackets ([]) are in the right place.

    Also make sure the image ID is an image that you found by searching the free images collection and that you have used a standard size such as medium.

Next steps

That's all you need to get started with the Shutterstock API. From here, you can use all kinds of programming languages and API clients to build applications that use Shutterstock's media and services. For more information about the API, see https://developers.shutterstock.com, and the endpoint reference and code samples in the API reference.

Lots of example code is available on GitHub at https://github.com/shutterstock, such as an example of how to authenticate with the API in JavaScript: shutterstock-oath-example.

Vi har over 475 000 000 ressurser på Shutterstock.com per 30. november 2023.

© 2003–2024 Shutterstock, Inc.