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 swagger specification 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 obtaining an api token docid vr6prldwhulaweklzwol of a factry historian user with the according privileges the uuid of the organizations 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 manage time series databases docid\ yhpic w9fthy09n103qcd listing existing databases manage collectors docid\ pc5 4ldexbiryshmxcdph creating a collector and listing existing collectors manage measurements docid\ ocuypopzcouihwwzc vj7 creating, updating and deleting measurements query time series data docid\ exh60uruui0st8sb4feiq read data from measurements write time series data docid\ ilwscu3w3jwb hkiy3uxj write data to measurements query event data docid\ khpjujdtdqy0189xxhan3 read data from events write event property data docid 4isqqqnf9umfyvthrdu0w 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