Python examples
introduction this guide shows how to read and write data with the factry historian api using python for a full overview of the factry historian api requests is available in the docid\ uwuiepl2xhh1xxzwejzqk target audience this guide is written for technical users who are comfortable working with a {{rest api}} and basics commands on a {{cli}} prerequisites python 3 9 or newer an docid vr6prldwhulaweklzwol of a factry historian user with the according privileges the uuid of the docid\ odmadpifqlvqbpgfarnxi in factry historian line 1 creates a virtual python environment (choose the package manager pip or uv ) line 2 activates the virtual environment for linux or mac replace with command source venv/bin/activate pip python m venv venv venv/bin/activate pip install upgrade pip pip install requests uv uv venv venv\scripts\activate uv pip install requests lines 3 and 4 upgrade the package manager and install the requests package import packages and create api session in your environment, set the following variables replace base url with your factry historian wepage url + /api replace org uuid with your factry historian organization uuid replace token with your factry historian api token it is recommended to keep sensitive values out of your code by using environment variables this code imports the necessary python packages and sets the base url , org uuid and token from the environment variables it creates an api session and sets the request headers in the session to be able to re use them over multiple requests import os import json import requests from typing import list, dict, any, optional base url = os environ get("historian base url", "https //historian mycompany com 18000/api") org uuid = os environ\["historian org uuid"] token = os environ\["historian api token"] session = requests session() session headers update({ "content type" "application/json", "accept" "application/json", "x organization uuid" org uuid, "authorization" f"bearer {token}" }) api interaction the code above serves as a prerequisite for the following examples docid 64sumjc8mtvauv2wzv13s listing existing databases docid\ xyild8ueb7as2mb qxt3i creating a collector and listing existing collectors docid\ wuysvoq7f1sx7qxo0i5t9 creating, updating and deleting measurements docid\ geu20 exrrt83tpzbib43 read data from measurements docid 9prbtgrac1 y8veo74xqy write data to measurements docid\ fgo3a3azwujehs2hclddp read data from events docid\ juujt1nhbvnsxankv0le6 create or update event data the examples list https //historian mycompany com 18000/api as the base url (replace with your own) and use a request timeout of 30 seconds
