If you don’t want to run your program in FarmBot OS (running code as a Farmware makes the process easier by providing the token for you), you will have to get a token and use that to send the number to the Web App.
To get your token, open the FarmBot Web App and use Ctrl + Shift + J
to open the JavaScript console. Paste store.getState().auth.token.encoded
and press Enter
. Copy the output string (between quotes), and replace paste_token_here
in the code below with the string.
import json
import requests
TOKEN = 'paste_token_here'
NUMBER = 100
headers = {'Authorization': 'Bearer ' + TOKEN,
'content-type': 'application/json'}
data = json.dumps({'message': 'Reported number: ' + str(NUMBER)})
response = requests.post('https://my.farmbot.io/api/logs',
headers=headers, data=data)
Alternatively, you could do it all in the code as shown (if you don’t mind storing your password in the code):
#!/usr/bin/env python
'''Report a number to the FarmBot Web App.'''
import json
import requests
# Inputs:
NUMBER = 100
EMAIL = 'farmbot@account.email'
PASSWORD = 'password'
# Get your FarmBot Web App token.
headers = {'content-type': 'application/json'}
user = {'user': {'email': EMAIL, 'password': PASSWORD}}
payload = json.dumps(user)
response = requests.post('https://my.farmbot.io/api/tokens',
headers=headers, data=payload)
TOKEN = response.json()['token']['encoded']
# Send the number to the FarmBot Web App logs.
headers = {'Authorization': 'Bearer ' + TOKEN,
'content-type': 'application/json'}
data = json.dumps({'message': 'Reported number: ' + str(NUMBER)})
response = requests.post('https://my.farmbot.io/api/logs',
headers=headers, data=data)