Last active
July 23, 2023 21:37
-
-
Save TimHeckel/8f10b1d1f6b00717415e0096cd3d16df to your computer and use it in GitHub Desktop.
python_slatebox_api.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# you will need to pip install requests | |
import requests | |
import hmac | |
import hashlib | |
import datetime | |
import base64 | |
baseUrl = "https://api.slatebox.com" | |
publicKey = "PUBLIC_KEY_HERE" | |
secretKey = "SECRET_KEY_HERE" | |
now = datetime.datetime.now() | |
timestamp = now.strftime("%Y-%m-%dT%H:%M:%SZ") | |
msg = f"{publicKey}:{timestamp}" | |
# Compute the HMAC of the message using the SHA-256 hash function | |
hmac_value = base64.b64encode( | |
hmac.new( | |
secretKey.encode("utf-8"), | |
msg.encode("utf-8"), | |
hashlib.sha256, | |
).digest() | |
).decode() | |
auth = f"{publicKey}:{hmac_value}" | |
# print(timestamp) | |
# print("auth", f"{publicKey}:{timestamp}", hmac_value) | |
# print("header", base64.b64encode(auth.encode()).decode()) | |
headers = { | |
"authorization": base64.b64encode(auth.encode()).decode(), | |
"accept": "application/json", | |
"timestamp": timestamp, | |
"content-type": "application/json", | |
} | |
body = { | |
"imageId": "image_1", | |
"type": "scratch", # url|template | |
"prompt": "Provide the long-form prompt here", | |
"async": True, # if True, then the call will return immediately with an id that you can correlate with your webhook calls; if False, then the HTTP post will return the slate creation information at the end | |
"userId": "userId", # the user id to assign this slate to -- you can get by calling /organization/users (see below) | |
"tone": "witty", # any one-word "tone" description you like (e.g., enthusiastic, etc) | |
"levelOfDetail": "5", # 10 is the most detail, 1 is the least | |
"wordsPerNode": "25", # no limit, but 25 is a good start | |
"themeId": "ocean", # these correspond to the "themes" in slatebox | |
"webhooks": { # optional, but if you use async: True, then you will want to provide webhooks - these are called when the image is completed or failed -- they will include the detail you want. The site webhook.site makes it easy to test these POSTS | |
"onComplete": "https://webhook.site/58dc9462-2e64-488b-a539-c40b5df2d8bc", | |
"onError": "https://webhook.site/58dc9462-2e64-488b-a539-c40b5df2d8bc", | |
}, | |
} | |
url = f"{baseUrl}/v1/slate/create" | |
resp = requests.post(url, headers=headers, json=body) | |
res = resp.json() | |
print(res) # {'id': 'image_1', 'final': True, 'error': False, 'pngUrl': 'https://files.slatebox.com/7286352b_1690147840542.png', 'slateUrl': 'https://app.slatebox.com/canvas/7286352b'} | |
######## uncomment the below call to get the users attached to your org - you can use the userId of the below call when you make the createSlate call above to produce the slate for the user -- all slates will still be accessible in slatebox under the "team slates" page | |
# url = f"{baseUrl}/v1/slate/create" | |
# resp = requests.get(url, headers=headers) | |
# res = resp.json() | |
# print(res) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment