# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda env:planet]
#     language: python
#     name: conda-env-planet-py
# ---

# # Introduction to the Planet API
#
# This notebook contains basic code to interact with the Planet API

# +
# Load modules (install if necessary)
import os
import requests
import geojsonio
import planet

# Import Planet config script
import planet_config
# -

# ## Authentication
#
# The first step is to authenticate our connection to the Planet API using our Planet API key. To keep this key secret, we first export it as an environment variable on our system using `export PL_API_KEY="<MY API KEY>"`

# RUN AFTER PUTTING IN API KEY 
os.environ['PL_API_KEY'] = planet_config.MY_API_KEY
print(os.getenv('PL_API_KEY'))

# Create API key variable from environment variable
PLANET_API_KEY = os.getenv('PL_API_KEY')


# Helper function to print formatted JSON using the json module
def p(data):
    print(json.dumps(data, indent=2))


# ### First Request
# To get started, we need to start a session and authenticate it using the Planet API base URL and our API key

# +
# Setup Planet Data API base URL
URL = "https://api.planet.com/data/v1"

# Setup the session
session = requests.Session()

# Authenticate
session.auth = (PLANET_API_KEY, "")

# Make a GET request to the Planet Data API
res = session.get(URL)
# -

# Check the status of the API response, which should be `200` to indicate that everything is ok.

# Check status of request
res.status_code

# ### Check Quota Usage
#
# The Planet Research & Education Program provides researchers with a monthly quota of 10,000 km^2 of imagery. To make sure we are using our quota effectively, we should regularly check how much quota we have remaining.

URL = "https://api.planet.com/auth/v1/experimental/public/my/subscriptions"
res = session.get(URL)

# Printing the JSON response shows us all the information about our subsciption, including the `quota_sqkm` and `quota_used` values.

res.json()



StarcoderdataJupyterScriptsDedupFiltered