Aller au contenu principal
Version : la plus récente

Python Quickstart

This guide walks you through authenticating to the VaultPAM API and making your first request using Python.

Prerequisites:

  • Python 3.8 or later
  • requests library: pip install requests
  • A VaultPAM account with API access enabled

Step 1 — Install the requests library

pip install requests

Step 2 — Authenticate

import getpass
import os
import requests

BASE_URL = os.environ.get("VAULTPAM_BASE_URL", "https://api.vaultpam.com")

email = input("Email: ")
password = getpass.getpass("Password: ") # Never commit credentials to source control

response = requests.post(
f"{BASE_URL}/api/v1/auth/login",
json={"email": email, "password": password},
)
response.raise_for_status()

auth_data = response.json()
# Never commit — see auth-flow.md#token-storage
token = auth_data["access_token"]
expires_in = auth_data["expires_in"]
print(f"Authenticated. Token expires in {expires_in} seconds.")
Never commit tokens

Store token in a secrets manager or environment variable — never hardcode credentials. See Token Storage.


Step 3 — Get your active organization

org_response = requests.get(
f"{BASE_URL}/api/v1/org/memberships",
headers={"Authorization": f"Bearer {token}"},
)
org_response.raise_for_status()

org_data = org_response.json()
org_id = org_data["active_org_id"]
print(f"Active org: {org_id}")

Step 4 — List your safes

safes_response = requests.get(
f"{BASE_URL}/api/v1/safes",
headers={
"Authorization": f"Bearer {token}",
"X-Active-Org-Id": org_id,
},
)
safes_response.raise_for_status()

safes = safes_response.json()
print(f"Found {len(safes)} safe(s):")
for safe in safes:
print(f" - {safe.get('name', safe.get('id'))}")

Complete example

import getpass
import os
import sys
import requests

BASE_URL = os.environ.get("VAULTPAM_BASE_URL", "https://api.vaultpam.com")


def get_session(base_url: str) -> tuple[str, str]:
"""Authenticate and return (token, org_id)."""
email = input("Email: ")
password = getpass.getpass("Password: ") # Never commit — see auth-flow.md#token-storage

resp = requests.post(f"{base_url}/api/v1/auth/login", json={"email": email, "password": password})
resp.raise_for_status()
token = resp.json()["access_token"]

org_resp = requests.get(f"{base_url}/api/v1/org/memberships", headers={"Authorization": f"Bearer {token}"})
org_resp.raise_for_status()
org_id = org_resp.json()["active_org_id"]

return token, org_id


def list_safes(base_url: str, token: str, org_id: str) -> list:
resp = requests.get(
f"{base_url}/api/v1/safes",
headers={"Authorization": f"Bearer {token}", "X-Active-Org-Id": org_id},
)
resp.raise_for_status()
return resp.json()


if __name__ == "__main__":
try:
token, org_id = get_session(BASE_URL)
safes = list_safes(BASE_URL, token, org_id)
print(f"Safes ({len(safes)}):")
for s in safes:
print(f" {s.get('name', s.get('id'))}")
except requests.HTTPError as exc:
print(f"API error {exc.response.status_code}: {exc.response.text}", file=sys.stderr)
sys.exit(1)

Error handling

raise_for_status() raises requests.HTTPError on 4xx/5xx responses. Access exc.response.status_code and exc.response.text for details:

CodeMeaning
401 UnauthorizedToken is missing, expired, or invalid. Re-authenticate.
403 ForbiddenToken lacks the required scope for this endpoint.
429 Too Many RequestsRate limit exceeded. Check retry-after header.
500 Internal Server ErrorTransient server error. Retry with backoff.

Rate limit awareness

response = requests.get(
f"{BASE_URL}/api/v1/safes",
headers={"Authorization": f"Bearer {token}", "X-Active-Org-Id": org_id},
)
remaining = response.headers.get("x-ratelimit-remaining")
print(f"Rate limit remaining: {remaining}")

See Rate Limits for backoff strategies.


Next steps