Making requests to the Density API is easy. Use our docs, available at https://docs.density.io, to understand the various objects you can request.

Two notes:

  1. These code snippets assume you are using Python3. 
  2. Replace TOKEN with your Density API Token (found on Dashboard)


In this example, we'll show you how to fetch the current count for all spaces from the Density API.

import requests


# Setup your API request parameters
TOKEN = "REPLACE_WITH_YOUR_DENSITY_API_TOKEN"
headers = {
  "Content-Type": "application/json",
  "Authorization": f"Bearer {TOKEN}"
}
url = "https://api.density.io/v2/spaces"

# Execute the API Request
response = requests.get(url, headers=headers)
json_data = response.json()

# Iterate through the data with python
# Display it in the console
for result in json_data["results"]:  
    print(f'{result["name"]}: {result["current_count"]}')



In this example, we'll show you how to fetch the count data for a specific space, over a given time range, from the Density API.

import requests
import pandas as pd


# Setup your API request parameters
TOKEN = "REPLACE_WITH_YOUR_DENSITY_API_TOKEN"
SPACE_ID = "REPLACE_WITH_A_SPACE_ID (spc_**)"
START_TIME = "2020-02-19T15:00:00Z"
END_TIME = "2020-02-21T15:00:00Z"

# Setup your API Request headers and query params
headers = {
  "Content-Type": "application/json",
  "Authorization": f"Bearer {TOKEN}"
}
query_params = f"start_time={START_TIME}&end_time={END_TIME}"
url = f"https://api.density.io/v2/spaces/{SPACE_ID}/counts?{query_params}"

# Execute the API Request
response = requests.get(url, headers=headers)
json_data = response.json()

# Load the data into a pandas dataframe
# Visualize in the console
data = json_data["results"]
df = pd.DataFrame(data)
print(df)

Did this answer your question?