Example Client

Once you have generated a token, you can try out the API to list vaults. The following implementation is a non-binding example to experience the API.

1. Define a RestClient:

"""
Waltrex API Client - Python Example
-----------------------------------
This client handles authentication and authenticated requests to the Waltrex API
in a reusable and extensible way.

Requires: pip install requests
"""

import os
import requests
import json

class WaltrexClient:
    def __init__(self):
        # Configuration from environment variables or defaults
        self.base_url = os.getenv("WALTREX_API_URL", "https://api.stage.waltrex.io")
        self.email = os.getenv("WALTREX_EMAIL")
        self.passcode = os.getenv("WALTREX_PASSCODE")

        self.token = None
        self.csrf_token = None
        self.authenticate()

    def authenticate(self):
        """
        Executes the full authentication flow.
        """
        flow_id = self._start_login_flow()
        self._confirm_passcode(flow_id)

    def _start_login_flow(self):
        """
        Step 1: Initiate the login flow and retrieve the flow ID.
        """
        url = f"{self.base_url}/irs/self-service/v1/flows/business-user/login/api-email-v1"
        response = requests.get(url)
        response.raise_for_status()
        return response.json()["data"]["id"]

    def _confirm_passcode(self, flow_id):
        """
        Step 2: Send email and passcode to obtain the session tokens.
        """
        url = f"{self.base_url}/irs/self-service/v1/flows/{flow_id}/logins"
        payload = {
            "email": self.email,
            "passcode": self.passcode
        }
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json"
        }
        response = requests.patch(url, headers=headers, json=payload)
        response.raise_for_status()
        session = response.json()["data"]["session"]
        self.token = session["token"]
        self.csrf_token = session["csrfToken"]

    def request(self, method: str, endpoint: str, json_data=None, params=None):
        """
        Perform an authenticated request.
        """
        if not self.token:
            raise Exception("Not authenticated. Call authenticate() first.")

        url = f"{self.base_url}{endpoint}"
        headers = {
            "Authorization": f"Bearer {self.token}",
            "X-IRS-CSRF-Token": self.csrf_token,
            "Accept": "application/json",
            "Content-Type": "application/json"
        }

        response = requests.request(method.upper(), url, headers=headers, json=json_data, params=params)
        response.raise_for_status()
        return response.json()

    def get(self, endpoint: str, params=None):
        return self.request("GET", endpoint, params=params)

    def post(self, endpoint: str, json_data=None):
        return self.request("POST", endpoint, json_data=json_data)

    def patch(self, endpoint: str, json_data=None):
        return self.request("PATCH", endpoint, json_data=json_data)

    def delete(self, endpoint: str):
        return self.request("DELETE", endpoint)

# ----------------------------------------
# Usage example
# ----------------------------------------

if __name__ == "__main__":
    client = WaltrexClient()
    response = client.get("/wallets/private/v1/current-vault-networks")
    print(json.dumps(response, indent=2))

2. Setup General Parameters and run the API call:

  self.base_url = os.getenv("WALTREX_API_URL", "https://api.stage.waltrex.io")
  self.email = os.getenv("WALTREX_EMAIL")
  self.passcode = os.getenv("WALTREX_PASSCODE")