Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to work with API
The purpose of this code is to show how to work with API.
You'll find API documentation at and
import base64
import requests
from time import sleep
secret_access_key = '-- ask for one at --'
def send_for_identificattion(file_names):
files_encoded = []
for file_name in file_names:
with open(file_name, 'rb') as file:
params = {
'latitude': 49.194161,
'longitude': 16.603017,
'week': 23,
'images': files_encoded,
'key': secret_access_key,
'parameters': ["crops_fast"]
# see the docs for more optinal atrributes; for example 'custom_id' allows you to work
# with your own identifiers
headers = {
'Content-Type': 'application/json'
response ='', json=params, headers=headers)
if response.status_code != 200:
raise("send_for_identificattion error: {}".format(response.text))
# this reference allows you to gather the identification result (once its ready)
return response.json().get('id')
def get_suggestions(request_id):
params = {
"key": secret_access_key,
"ids": [request_id]
headers = {
'Content-Type': 'application/json'
# To keep it simple, we are pooling the API waiting for the server to finish the identification.
# The better way would be to utilise "callback_url" parameter in /identify call to tell the our server
# to call your's server enpoint once the identificatin is done.
while True:
print("Waiting for suggestions...")
resp ='', json=params, headers=headers).json()
if resp[0]["suggestions"]:
return resp[0]["suggestions"]
# more photos of the same plant increases the accuracy
request_id = send_for_identificattion(['photo1.jpg','photo2.jpg'])
# just listing the suggested plant names here (without the certainty values)
for suggestion in get_suggestions(request_id):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.