Menu AuthGuardHelp centre
Portuguêsauthguard.pt

Developers · SDKs

Python SDK

  • 1 min read
  • SaaS · Self-hosted

The authguard-sdk package (imported as authguard) uses PyJWT for JWTs.

Install

It is not on PyPI yet. From the repository:

pip install ./sdks/python

Create a request and wait

import os
from authguard import IntentsClient, WaitTimeoutError

client = IntentsClient(
    base_url="https://identity.example.com",
    client_id="1c9a2b3d-4e5f-4a6b-9c7d-8e9f0a1b2c3d",
    client_secret=os.environ["AUTHGUARD_CLIENT_SECRET"],
)

result = client.create_intent(
    {"operationType": "invoice.pay", "subjectEmail": "ana@example.com",
     "externalReference": "INV-42",
     "fields": {"amount": {"amount": "1250.00", "currency": "EUR"}}},
    idempotency_key="erp-INV-42-attempt-1",
)
print(result.intent["comparisonCode"])   # show it next to the operation

try:
    decided = client.wait_for_decision(result.intent["intentId"], timeout=900)
except WaitTimeoutError:
    client.cancel_intent(result.intent["intentId"])
    raise

result.replayed is True when the key had already been used with the same body.

Verify a webhook (Flask)

from authguard import verify_webhook, WebhookVerificationError

event = verify_webhook(WEBHOOK_SECRET, request.headers, request.get_data())

A failure raises WebhookVerificationError; answer 400. Deduplicate on webhook-id.

Verify the receipt

verify_intent_digest(intent["intentJson"], intent["intentDigest"]) and check_receipt(intent["receipt"], intent_id=…, intent_digest=…, state=…).

OpenID Connect

OidcClient handles discovery, the PKCE URL, the code exchange and ID token validation.

Errors

API failures raise ApiError with the HTTP status and the stable code; wait_for_decision raises WaitTimeoutError when the wait ends, and cancel_intent raises a 409 ApiError when the request was decided meanwhile. Branch on the code, not on the message text; see Errors and limits.

Error codes in this article

Article referencedev-sdk-pythonquote it to support to identify this article. Checked against the product at commit 5f6607a8.