İçeriklere geç

Shutterstock API tutorial: cURL

This version of the tutorial walks you through the steps using the cURL command-line program. 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 cURL

The cURL program is available for most operating system command-line terminals. You can use it for many types of computer communication, but in the context of this tutorial, it sends requests to the Shutterstock API and receives responses. cURL is installed on MacOS and most versions of Linux by default.

Follow these steps to verify that cURL is installed and to give it your token:

  1. To see if cURL is installed on your computer, open a command-line window and run the command curl --help. If the command prints information about the command and its parameters, as in the following picture, cURL is installed.

  2. If cURL is not installed, use the package manager for your operating system or distribution to install it. For Windows, use a package manager such as Chocolatey, MSYS2, or Scoop, or download and install it from curl.se/windows.

  3. 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.

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

    curl -X GET https://api.shutterstock.com/v2/user \
    -H "Accept: application/json" \
    -H "Authorization: Bearer $SHUTTERSTOCK_API_TOKEN"

    If cURL can use the token to authenticate to the API, the terminal window prints information about your user account. This response from the API 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}
  5. Optional: Install and use the jq program to pretty-print the output.

    The jq program manipulates JSON data, such as the responses from the API. You can install it through your operating system's package manager or from the jq web site.

    After you install jq, you can use it to improve the formatting of the API responses. To format a response, use the pipe character (|) to send it to jq. For example, this command runs the same user information command as the previous step, but it uses the pipe character to send the output to jq and a dot to tell jq to process the entire response:

    curl -X GET https://api.shutterstock.com/v2/user \
    -H "Accept: application/json" \
    -H "Authorization: Bearer $SHUTTERSTOCK_API_TOKEN" | jq .

    You can use jq to pretty-print any JSON response from the API and to simplify the output. For example, if you want to show only the user name and ID from the response, you can use the jq filter .username,.id, as in this example:

    curl -X GET https://api.shutterstock.com/v2/user \
    -H "Accept: application/json" \
    -H "Authorization: Bearer $SHUTTERSTOCK_API_TOKEN" | jq .username,.id

    The result prints only the username and id fields from the API response.

Now cURL is set up and you can use it to access the endpoints in the API. To find the right endpoints for your tasks, you can refer to the Shutterstock API reference and select the cURL tab for examples of accessing each endpoint with cURL.

Part 3: Searching for images

If you're familiar with searching for images on shutterstock.com, searching with the REST API is similar. 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 GET /v2/images/search endpoint. Here's an example that searches for photos with kites in them:

    curl "https://api.shutterstock.com/v2/images/search" \
    -X GET \
    -G \
    --data-urlencode "query=kites" \
    --data-urlencode "image_type=photo" \
    --data-urlencode "page=1" \
    --data-urlencode "per_page=5" \
    --data-urlencode "sort=popular" \
    --data-urlencode "view=minimal" \
    --header "Authorization: Bearer $SHUTTERSTOCK_API_TOKEN"

    The search uses the keyword "kites" and selects the first 5 results, sorted by popularity. 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:

    curl "https://api.shutterstock.com/v2/images/search" \
    -X GET \
    -G \
    --data-urlencode "query=kites" \
    --data-urlencode "image_type=photo" \
    --data-urlencode "page=1" \
    --data-urlencode "per_page=5" \
    --data-urlencode "sort=popular" \
    --data-urlencode "view=minimal" \
    --header "Authorization: Bearer $SHUTTERSTOCK_API_TOKEN" \
    | 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 GET /v2/images/{id} endpoint. 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:

    curl "https://api.shutterstock.com/v2/images/1843607236" \
    -X GET \
    -H "Accept: application/json" \
    -H "Authorization: Bearer $SHUTTERSTOCK_API_TOKEN"

    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 endpoints like GET /v2/images/{id}/similar and GET /v2/images/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 API, run this command:

    curl "https://api.shutterstock.com/v2/user/subscriptions" \
    -X GET \
    -H "Authorization: Bearer $SHUTTERSTOCK_API_TOKEN"

    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 POST /v2/images/licenses endpoint and include the JSON data as the request body.

    DATA='{
      "images": [
        {
          "image_id": "1843607236",
          "subscription_id": "s12345678",
          "size": "medium"
        }
      ]
    }'
    
    curl -X POST "https://api.shutterstock.com/v2/images/licenses" \
    -H "Authorization: Bearer $SHUTTERSTOCK_API_TOKEN" \
    -H 'Content-Type: application/json' \
    -d "$DATA"

    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 POST /v2/images/licenses/{id}/downloads 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.

30 Kasım 2023 itibarıyla Shutterstock.com’da 475.000.000’dan fazla varlık var.

© 2003-2024 Shutterstock, Inc.