API Mechanics: Get Started with Subscriptions

last updated: September 17, 2024

API mechanics

Get Started with Planet APIs explains how to get your API key and how to work with Planet APIs in general. Once you have your API key, you are ready to work with the Subscriptions API. You can test your API key at the command line by doing the following cURL request, where your-API-key is the string you copied from your Accounts > My Settings page.

curl -u your-API-key: https://api.planet.com/subscriptions/v1/

Authentication

The Planet API uses Basic HTTP Authentication and requires that you have a Planet API key. Once you're signed up, you can find your API key in your account settings. Only one value is needed, so instead of using a username and password, you authenticate by setting username to your API key.

You could pass the API key as a variable in your code, but doing so makes it easy for others to see your key--not a good privacy and security practice. So the Python example, below, presumes you've stored your API key in a .env file at the root of your project.

You create a .env file and place your API string there:

PL_API_KEY = "[The API string from the My Settings section of your Planet Account page]"

Now, you are ready to pull your API key into your Python code without exposing it in the code. Assuming your Python file is in the same directory as your .env file, you import the dotenv package and call dotenv_values(".env").

Start by importing the packages used in these examples.

Note

If you get an error, you may need to install Python packages. For example, for dotenv, you might use the command line with pip install python-dotenv, depending on how you set up Python on your system.

# dotenv handles environment variables, such as your API key
from dotenv import dotenv_values

# requests handles sending and receiving requests,
# including authenticating with the server
import requests
from requests.auth import HTTPBasicAuth

# Use json to gather the info for your request
import json

# Get your Planet API Key from an environment variable
DOT_ENV_VALS = dotenv_values(".env")

# Your Planet API Key
API_KEY = DOT_ENV_VALS["PL_API_KEY"]

Next, you want to create a constant for the Planet Subscriptions API base URL. This is the location where the Planet service receives your requests and returns data to you. All requests are additional parameters you append to this base URL.

# Planet's Subscriptions API base URL for making restFUL requests
BASE_URL = "https://api.planet.com/subscriptions/v1"

Now your are ready to make a call to the Subscriptions API.

# Confirm that your API key is valid

# Set an authentication value
auth = HTTPBasicAuth(API_KEY, '')

# Do a GET request and the response
response = requests.get(BASE_URL, auth=auth)
# if you're using a Python notebook, you can just use `response` instead of print(response)
print(response)

You should see the response: <Response [200]>.

Permissions

Depending on your account type, you may have different permissions for different products. For Subscriptions API, you must have access to a particular area of access (AOA). Your area of interest (AOI) must be within your area of access. The person at your organization who has set up your account can provide you with the geometries of the AOAs you have access to. Alternatively, you can confirm this AOA with your Planet CSM.

The following example compares the area of interest with the area of access provisioned for you.

from shapely.geometry import shape

# Area of Interest (AOI) - Phoenix, AZ
AOI = {
    "type": "Polygon",
    "coordinates": [[[-112.448776,33.23932599999999],
                        [-112.448776,33.95612],
                        [-111.72052,33.95612],
                        [-111.72052,33.23932599999999],
                        [-112.448776,33.23932599999999]]]
      }

# Area of Interest (AOI) - US
AOA = {
    "type": "Polygon",
    "coordinates": [[[ -126.9504, 46.5531 ],
                        [ -54.1833, 46.7628 ],
                        [ -77.8798, 24.3246 ],
                        [ -94.6561, 23.9051 ],
                        [ -124.2242, 34.1806 ],
                        [ -126.9504, 46.5531 ]]]
      }

# Determine if the AOI is inside the AOA
def is_inside(AOI, AOA):
    smaller_shape = shape(AOI)
    larger_shape = shape(AOA)

    return smaller_shape.within(larger_shape)

result = is_inside(AOI, AOA)
print(result)  # Expect a True result

Most Planet API responses contain a _links object that contains a list of hyperlinks to itself and related data. You are encouraged to rely on these links rather than constructing the links yourself.

The most common _link is _self, which is a self reference. When an API response is paginated, _links will contain _next and _prev references.

Pagination

The Planet API paginates responses to limit the results, making them easier to work with. The first GET request will yield the first page along with _links representing the location of the _next page. Following the _next link will return another page of results. This process may be repeated until the _next link is no longer returned, which indicates the last page of results.

The following _links are provided in the response to facilitate pagination:

  • _self - The canonical location of the current page.
  • _first - The initial page.
  • _next - The page that logically follows the current page.
  • _prev - The page that logically precedes the current page.

Pagination example

Web services, such as the Planet API, usually limit the amount of data you can pull in one request. The limit for the Subscriptions API is 20. After you've authenticated to the server and gotten a <Response [200]>, as discussed in the Authentication section, you can get an indication if there are likely more than 20 results in any given response. For example, by getting the response object subscriptions field, you can get the length of results. If the number is over 20, you will want to paginate through all of your subscriptions, returning 20 at a time.

# 20 or more subscriptions?
subscriptions = response.json()['subscriptions']
print(len(subscriptions))

You may have more subscriptions, imagery, or Planetary Variables than you can download in one request.

The following example pages through existing subscriptions, gets the first list of subscriptions, and goes on to the next bundle of subscriptions if it exists:

BASE_URL = 'https://api.planet.com/subscriptions/v1/'

def get_subscriptions(url):
    try:
        resp = requests.get(url, auth=auth)
        resp.raise_for_status()  # Raise an exception for non-2xx status codes
        response_data = resp.json()
        subscriptions = response_data['subscriptions']
        subscription_count = len(subscriptions)
        print(f'Available subscriptions: {subscription_count}')
        if '_links' in response_data and 'next' in response_data['_links']:
            next_url = response_data['_links']['next']
            print(f'More subscriptions are available through pagination links: {next_url}')
            return subscriptions + get_subscriptions(next_url)
        else:
            return subscriptions
    except requests.exceptions.RequestException as e:
        print(f'Error occurred during API request: {e}')
    except (KeyError, json.JSONDecodeError) as e:
        print(f'Error occurred while processing JSON response: {e}')
    return []

all_subscriptions = get_subscriptions(BASE_URL)

# Process all subscriptions
for subscription in all_subscriptions:
    print(subscription)

Example Subscriptions API request

So assuming you have imported the basic packages and authenticated, as shown in the Authentication section, and you have confirmed that your AOI is within your AOA, as discussed in the Permissions section, you are ready to set up your subscription and pass it to the Subscriptions API service as explained in Subscribing to Planetary Variables.

A note about cloud delivery

For large datasets or for sharing or working online, you probably want to deliver your data to the cloud. Let's start out by defining some variables for cloud delivery.

Credentials and Permissions

Whatever credentials you use for your cloud storage, you want to make sure you are securing those credentials. Make sure the account you use for cloud delivery is secure and is granted the most restrictive permission. In this example, we use a Google Cloud Service Agent with a custom role that grants only three permissions, as in storage.objects.create, storage.objects.get, and storage.objects.delete permissions. Also, we take an additional step of base64-encoding the credentials, as described in Delivery to Google Cloud Storage.

# Your GCS project ID in which your bucket is located
GCS_PROJECT_ID = DOT_ENV_VALS["GCPROJECTID"]

# Your GCS bucket name that resides in the GCS project
GCS_BUCKET_NAME = DOT_ENV_VALS["GCSBUCKET"]

# Your GCS service agent's JSON credentials that you Base64-
# encoded and stored in the .env file
GCS_CREDS_B64 = DOT_ENV_VALS["GCSCREDS"]

 

Learning Resources

Get Started with Planet APIs. Checkout Planet notebooks on GitHub, such as the Subscriptions tutorials: subscriptions_api_tutorial. See also: Land Surface Temperature Technical Specification, Soil Water Content Technical Specification, and Crop Biomass Technical Specification. Find a collection of guides and tutorials on Planet University.



We are continually working to improve our technical documentation and support. Please help by sharing your experience with us.

Send Us Your Feedback