Skip to main content
Crusoe Support Help Center home page
Crusoe

How-To Query Crusoe API using a Python Script

Irman Mashiana
Irman Mashiana
Updated

Introduction

The following article outlines the steps to query the Crusoe API using a Python script.

 

Prerequisite 

Instructions

Below is an example to query the Crusoe API endpoints via Python. You can learn more about authenticated API requests here.

import hmac
import hashlib
import base64
import datetime
import requests
import json

api_access_key = "<INSERT_ACCESS_KEY>"
api_secret_key = "<INSERT_SECRET_KEY>"

request_path = "<INSERT_REQUEST_PATH>" # e.g., "/compute/images". see https://docs.crusoecloud.com/api/index.html
request_verb = "<INSERT_REQUEST_VERB>" # GET, PUT, POST, PATCH, DEL
query_params = "<INSERT_QUERY_PARAMS>" # if needed. empty otherwise.

# if there is a request body, e.g., with requests.post(..., json=body, ...)
# body = {
# "request_body_field_1" = "value",
# "request_body_field_2" = "value",
# }

signature_version = "1.0"
api_version = "/v1alpha5"

dt = str(datetime.datetime.now(datetime.timezone.utc).replace(microsecond=0))
dt = dt.replace(" ", "T")


payload = api_version + request_path + "\n" + query_params + "\n" + request_verb + "\n{0}\n".format(dt)
decoded = base64.urlsafe_b64decode(api_secret_key + '=' * (-len(api_secret_key) % 4))
signature = base64.urlsafe_b64encode(hmac.new(decoded, msg = bytes(payload, 'ascii'), digestmod=hashlib.sha256).digest()).decode('ascii').rstrip("=")

# make sure this method matches your HTTP verb above
response = requests.get('https://api.crusoecloud.com' + api_version + request_path + "?" + query_params, headers={'X-Crusoe-Timestamp': dt, 'Authorization': 'Bearer {0}:{1}:{2}'.format(signature_version, api_access_key, signature)})
data = response.text
mydata = json.loads(data)
print(json.dumps(mydata, indent=4))

1. Copy / Paste the above Python script in your favorite code editor

2. Update the api_access_key and the api_secret_key associated to your organization

     i. Using the UI: https://docs.crusoecloud.com/identity-and-security/managing-api-keys

     ii. If you've configured the Crusoe CLI, this can be listed by running the following command: 

$ cat ~/.crusoe/config

3. Update the request_path with the Crusoe endpoint (Ex. /capacities)

4. Update the request_verb with the API request method (Ex. "GET")

5. Update the query_params with the parameters as suggested in the API docs

     i. Important Note: Ensure the parameters are sorted alphabetically and all parameters are 

        separated with "&" (Ex. location=us-northcentral1-a&product_name=a100.8x)

6. Run the Python script

 

FAQ

1. Why does the quantity only appear as 1 or 0?

Answer: The API functions in binary mode, indicating whether capacity is available without disclosing specific quantities. A quantity of "1" indicates capacity is available, and "0" indicates capacity is unavailable. 

 

Related to

Was this article helpful?

0 out of 0 found this helpful

Still need help?

Our support team is ready to assist you with any questions.

Have more questions? Submit a request

Recently Viewed

Comments

0 comments

Article is closed for comments.