import base64
import json
import os
import urllib.request
import uuid
base_url = os.environ.get("TRUTHLOCKS_BASE_URL", "https://sandbox-api.truthlocks.com").rstrip("/")
payload = json.dumps({"subject": "demo-record", "message": "Example issuer claim"},
separators=(",", ":")).encode("utf-8")
body = {
"issuer_id": os.environ["TRUTHLOCKS_ISSUER_ID"],
"kid": os.environ["TRUTHLOCKS_KEY_ID"],
"alg": os.environ["TRUTHLOCKS_ALGORITHM"],
"content_type": "application/json",
"payload_b64url": base64.urlsafe_b64encode(payload).decode("ascii").rstrip("="),
}
# Retain this value and the same body if retrying this mint request.
idempotency_key = str(uuid.uuid4())
request = urllib.request.Request(
base_url + "/v1/attestations/mint",
data=json.dumps(body).encode("utf-8"),
headers={"Content-Type": "application/json",
"X-API-Key": os.environ["TRUTHLOCKS_API_KEY"],
"Idempotency-Key": idempotency_key},
method="POST",
)
with urllib.request.urlopen(request, timeout=30) as response:
record = json.load(response)
print("Attestation ID:", record["attestation_id"])
print("Recorded at (Unix seconds):", record["issued_at_unix"])