Python integration
Send a reading using Python's standard library.
On this page
What you need
Python 3, a numerical series and a device key stored in a private file.
How to do it
- Create a Gauge series and save its device key at ~/.config/metribeacon/device.key with private file permissions.
- Replace the example's series ID with your own. Prepare the value, timestamp and IDs once for each reading.
- Run the example and check accepted or duplicates in the response. Open the monitor to check the reading.
Good to know
The example uses a request timeout. For temporary errors, add a limited retry with Retry-After and keep the same prepared request. A new ID means a new reading.
import datetime
import json
import pathlib
import urllib.request
import uuid
key = pathlib.Path.home().joinpath(".config/metribeacon/device.key").read_text().strip()
batch = {"batch_id": str(uuid.uuid4()), "points": [{
"id": str(uuid.uuid4()), "series_id": "33333333-3333-4333-8333-333333333333",
"measured_at": datetime.datetime.now(datetime.timezone.utc).isoformat(),
"value": 21.5}]}
body = json.dumps(batch).encode()
request = urllib.request.Request("https://metribeacon.com/api/v1/ingest/points",
data=body, method="POST", headers={"Authorization": "Bearer " + key,
"Content-Type": "application/json"})
with urllib.request.urlopen(request, timeout=15) as response:
print(json.load(response))If something goes wrong
Check the HTTP error before reading the response as success. Fix 401, 403, 413 or 422 before retrying; do not print the key in diagnostics.
Related guides
- Send your own monitoring data
Send your own readings over HTTPS using the IDs and keys of your monitor.
- Quotas and fair use
Plan your monitoring within the shared limits of your account.