Skip to main content

Python

For Python 3, using the requests package. Install it with pip install requests.

The API must be enabled first, see Enabling the API. How a request is signed is explained in Authentication.

Client​

import hashlib
import hmac
import json
import time
import uuid
from urllib.parse import unquote

import requests

SERVER_URL = "http://localhost:19200"
API_TOKEN = "YOUR_API_TOKEN"


def fileflows_api(method, path, body=None):
"""Calls the FileFlows API, each request is signed with the API token (the token is never sent)"""
data = "" if body is None else json.dumps(body)
timestamp = str(int(time.time()))
nonce = uuid.uuid4().hex
body_hash = hashlib.sha256(data.encode("utf-8")).hexdigest() if data else ""
signed_path = unquote(path.split("?")[0])
string_to_sign = ":".join([method.upper(), signed_path, timestamp, nonce, body_hash])
signature = hmac.new(API_TOKEN.encode("utf-8"), string_to_sign.encode("utf-8"), hashlib.sha256).hexdigest()

headers = {"X-Timestamp": timestamp, "X-Nonce": nonce, "X-Signature": signature}
if data:
headers["Content-Type"] = "application/json"
response = requests.request(method, SERVER_URL + path, data=data.encode("utf-8") if data else None, headers=headers)
if not response.ok:
raise Exception(f"{response.status_code}: {response.text}")
return response.json() if response.text else None


# status = fileflows_api("GET", "/api/status")

Replace http://localhost:19200 with the address of your FileFlows server, and YOUR_API_TOKEN with the token from Config > API. Load the token from an environment variable, e.g. os.environ["FILEFLOWS_API_TOKEN"], rather than hard coding it.

Usage​

status = fileflows_api("GET", "/api/status")
print(status)

To send a JSON body, pass it as the body argument. The function serializes it once, and signs and sends that same JSON text.