Python
Install and use Nowgate's Python SDK, for Python 3.10 and later, on the standard library alone.
On this page
pip install nowgateThe SDK needs Python 3.10 or later and nothing outside the standard library. It is fully typed
(py.typed): every result is a TypedDict, checked by mypy and your editor.
Configure#
client = Nowgate(api_key=env("NOWGATE_API_KEY"), base_url=base_url, transport=counted)| Option | Default | What |
|---|---|---|
api_key | (needed) | Your API key. Its environment picks the API. |
base_url | from the key | Another API, such as a local engine. |
timeout | 60.0 | Seconds each attempt may wait for the network. |
max_retries | 2 | How many times a failed attempt is retried. |
transport | urllib | Your own HTTP client: see below. |
app_info | none | Your application, added to the User-Agent: my-shop/1.2. |
Every method also takes timeout= and max_retries=, and every POST idempotency_key=.
Calls#
Body fields and query parameters are keyword arguments with the API's own names, so a misspelt field fails at the call. Path IDs come first.
payment = client.payments.create(
network="tron",
asset="USDT",
amount="25.00",
order_id="1042",
idempotency_key=order_key,
)To read the status, the headers and the request's ID too, make the same call through
client.with_response:
again = client.with_response.payments.create(
network="tron",
asset="USDT",
amount="25.00",
order_id="1042",
idempotency_key=order_key,
)| Method | Operation |
|---|---|
client.status.retrieve() | Service and network status |
client.assets.list() | Supported networks and assets |
client.rates.list() | List rates |
client.rates.estimate() | Estimate a fiat price |
client.rates.min_amount() | Get an asset's minimum |
client.payments.create() | Create a payment |
client.payments.retrieve() | Retrieve a payment |
client.payments.list() and client.payments.list_all() | List payments |
client.payment_links.create() | Create a payment link |
client.payment_links.retrieve() | Retrieve a payment link |
client.payment_links.update() | Update a payment link |
client.payment_links.list() and client.payment_links.list_all() | List payment links |
client.logos.create() | Upload a logo |
client.balances.list() | List balances |
client.payout_destinations.create() | Add a payout destination |
client.payout_destinations.retrieve() | Retrieve a payout destination |
client.payout_destinations.disable() | Disable a payout destination |
client.payout_destinations.list() and client.payout_destinations.list_all() | List payout destinations |
client.payouts.create() | Create a payout |
client.payouts.retrieve() | Retrieve a payout |
client.payouts.cancel() | Cancel a payout |
client.payouts.list() and client.payouts.list_all() | List payouts |
client.payout_batches.create() | Create a batch of payouts |
client.payout_batches.retrieve() | Retrieve a payout batch |
client.payout_batches.list() and client.payout_batches.list_all() | List payout batches |
client.refunds.create() | Refund a payment |
client.refunds.retrieve() | Retrieve a refund |
client.refunds.cancel() | Cancel a refund |
client.refunds.list() and client.refunds.list_all() | List refunds |
client.forwarding.retrieve() | Retrieve your forwarding rules |
client.forwarding.update() | Set your forwarding rules |
client.plans.create() | Create a plan |
client.plans.retrieve() | Get a plan |
client.plans.update() | Update a plan |
client.plans.list() and client.plans.list_all() | List plans |
client.subscriptions.create() | Create a subscription |
client.subscriptions.retrieve() | Get a subscription |
client.subscriptions.update() | Update a subscription |
client.subscriptions.cancel() | Cancel a subscription at once |
client.subscriptions.rotate_link() | Replace the subscriber's link |
client.subscriptions.list() and client.subscriptions.list_all() | List subscriptions |
client.invoices.retrieve() | Get an invoice |
client.invoices.list() and client.invoices.list_all() | List invoices |
client.sandbox.simulate_payment() | Simulate a payer (sandbox) |
client.sandbox.simulate_payout() | Fail a payout (sandbox) |
client.sandbox.activate_payout_destination() | Activate a destination now (sandbox) |
client.sandbox.simulate_subscription() | Move a subscription's time (sandbox) |
client.sandbox.simulate_invoice() | Pay an invoice (sandbox) |
Errors#
try:
client.payments.create(network="tron", asset="USDT", amount="25.0000001")
except InvalidRequestError as err:
# err.code is "amount_too_precise"; each field's problem has its JSON pointer
pointer = err.errors[0]["pointer"]
check(err.code == "amount_too_precise", err.code)The exceptions are NowgateError, ApiError and its classes by status
(AuthenticationError, PermissionDeniedError, NotFoundError, ConflictError,
InvalidRequestError, RateLimitError, ServerError), APIConnectionError when there's no
answer, and SignatureVerificationError. See Errors.
Pages#
every = [payment["id"] for payment in client.payments.list_all()]list_all is a generator: it fetches nothing until you iterate, and stops when you do.
Amounts#
Amount fields take a decimal string, an int or a finite decimal.Decimal. A float is
refused with a TypeError before anything is sent. Amounts.to_units, Amounts.from_units and
Amounts.compare convert and compare amounts exactly. See Amounts.
Webhooks#
body = self.rfile.read(int(self.headers["Content-Length"])) # the raw body, exactly as received
signature = self.headers["Nowgate-Signature"]
try:
event = Webhook.construct_event(body, signature, secret)
except SignatureVerificationError:
self.send_response(400)
self.end_headers()
returnWebhook.verify_signature and Webhook.construct_event take the raw body as bytes or str:
in Flask, request.get_data(); in Django, request.body. See Webhooks.
Your own HTTP client#
A transport is a function from a nowgate.Request to a nowgate.Response. When there's no
answer, it raises an OSError (such as ConnectionError or TimeoutError), which the client
retries:
def counted(request: Request) -> Response:
global requests
requests += 1
return urllib(request) # or any HTTP client: raise an OSError when there's no answer