Webhooks for energy orders: events, signatures and retries
An energy order changes state while your code waits on it. The API can announce the steps you subscribe to, the energy landing and the order closing among them, with a signed POST request to an address you register. When your receiver does not accept a delivery, the API retries it for about 13 minutes. A receiver that works from its first day checks the signature over the raw body, refuses a timestamp more than five minutes away from its own clock, answers 2xx within five seconds, and drops any event whose event_id it has already handled.
Registering a receiver
One request registers an endpoint: POST /v1/webhooks with a url and the list of events to send there. The answer carries the signing secret, and no later answer shows it again. The url must use https, carry no credentials and resolve to a public host, so a receiver on localhost or in a private network is refused. A url holding an ASCII control character, usually a trailing newline read out of a file, is refused with 400 invalid_request, and details.position gives the offset of the first such character. An account holds at most five active endpoints, and a sixth gets 400 invalid_request with active_webhooks and limit in details.
The events and when each one is sent
There are 21 events to subscribe to, under six prefixes. A payout system wires the order events first (the order flow they report on).
| Event | Sent when |
|---|---|
order.ready | Energy has been delivered for the last item of the order. Carries duration_s, the lease the order was sold as, and send_before, one deadline shared by every transfer of a batch. |
order.item.sent | We saw the item's transfer on chain; txid is its hash. |
order.item.expired | The energy was delivered, the transfer did not happen, and the item is charged. |
order.completed, order.partially_completed, order.failed | The order has closed. Carries charged_trx and duration_s. |
order.repriced | A position was raised to the market just before it was bought, or a raise on a volume position was taken back. |
deposit.credited | A deposit of one TRX or more was credited to your balance. |
balance.low | Your balance is under the balance_low_threshold_trx you set in PATCH /v1/settings; zero switches the event off. |
topup.requested, topup.fulfilled, topup.expired | A request for TRX to your deposit address was opened, met or left to lapse. Sent only while auto_topup.enabled is on. |
maintenance.topup, maintenance.budget_exhausted, maintenance.paused | We delivered energy to an Auto-refill rule's wallet, the rule spent its daily budget, or the rule was paused. |
queue.started, queue.packet_delivered, queue.paused, queue.resumed, queue.completed, queue.stopped | A queue session moved through its life; only queue.packet_delivered, one per packet, moves money. |
One more event, webhook.test, arrives only when you ask for a test delivery. It cannot be subscribed to, so a handler that switches on the event name never takes a probe for a real event.
What a delivery carries
The body has four fields, event, event_id, sent_at and data. The API reference gives this example of order.ready:
{"event":"order.ready",
"event_id":"3f9a1d2e-6c47-4b1a-9f0e-8d5c2a7b4e13",
"sent_at":"2026-08-06T12:00:03Z",
"data":{"order_id":"ord_9c1f2a","status":"ready","duration_s":3600,
"send_before":"2026-08-06T12:59:57Z"}}
Three headers come with every delivery. X-NRG-Timestamp is Unix seconds, set again on each attempt. X-NRG-Signature is the hex HMAC-SHA256, keyed with your secret, of the timestamp, a dot and the raw body. X-NRG-Event-Id repeats the body's event_id. Look the names up without regard to case: a live delivery arrived as X-Nrg-Signature, and a receiver that matches the upper-case spelling letter for letter finds no signature. Treat a missing signature as a failed check.
Checking the signature and the timestamp
Compute the HMAC over the bytes exactly as they arrived. A body your JSON library parsed and encoded again will not match the signature. Compare the two values with a constant-time function. Then accept a timestamp within five minutes either side of your clock and refuse the rest: without that window, anyone holding one captured request could replay it forever. Every attempt carries its own timestamp, so a retry twelve minutes after the event still arrives fresh.
import hashlib, hmac, time
def verify(secret: str, timestamp: str | None, signature: str | None, raw_body: bytes) -> bool:
if not (timestamp and signature and timestamp.isascii() and timestamp.isdigit()):
return False
if abs(time.time() - int(timestamp)) > 300:
return False
expected = hmac.new(
secret.encode(), timestamp.encode() + b"." + raw_body, hashlib.sha256
).hexdigest()
return hmac.compare_digest(expected.encode(), signature.encode())
Answer 2xx within five seconds, then do the work
Any 2xx is accepted. Any other status fails the attempt. So does an answer slower than five seconds, no answer at all, or a redirect, since redirects are not followed. Queue the event on your side, answer, then do the work. A slow answer alone brings the event back as a retry.
Retries, and when a receiver is switched off
Each delivery of a subscribed event gets up to five attempts over about 13 minutes:
| Attempt | Sent |
|---|---|
| First | At once |
| Second | 5 seconds after the first attempt fails |
| Third | 30 seconds after the second attempt fails |
| Fourth | 2 minutes after the third attempt fails |
| Fifth | 10 minutes after the fourth attempt fails |
After the fifth failure the delivery is failed and is not tried again on its own. When 20 deliveries that were queued in the last 24 hours have each used up all five attempts, the endpoint becomes status: disabled and receives nothing more. Test deliveries never count towards the 20. A URL that resolves to a private or loopback address skips the retries and disables the endpoint at once.
When an endpoint is switched off, we email every user of the account, and GET /v1/webhooks shows disabled_at and disabled_reason. An account with no users, such as a pure API integration, gets no email; for it, GET /v1/webhooks is the only place the switch-off shows. The reasons today are too_many_failures, target_not_public and url_unusable. A null reason means the switch-off predates the field; treat the list as open. No API call switches an endpoint back on: register a new endpoint, with a new secret to deploy, or fix the receiver and ask support to switch the same endpoint back on with the same secret.
Duplicates, sequence and the event id
Every copy of an event, retried automatically or replayed at your request, carries the same event_id, so "have I seen this id" is the whole duplicate check. Read the id from the body, which the signature covers; the copy in the header is unsigned.
Events can also arrive out of sequence. Deliveries are retried independently, so an event whose first attempt failed can land after an event that was sent after it. In mode A, order.item.sent comes after order.completed by design, because the order closes when the energy is delivered and you send the transfer after that. topup.requested can arrive twice for one topup_request_id, and the second one supersedes the first. The current total_trx of a repriced order is read from GET /v1/orders/{order_id}; adding up the amounts of order.repriced events does not give it.
The delivery log, replays and the test event
GET /v1/webhooks/{webhook_id}/deliveries lists one endpoint's deliveries, newest first, each with its event_id. A null in response_status means no answer came at all. That field and last_error describe the latest attempt only, so on a delivery that got through in the end they show the attempt that worked; failed_attempts and the last_failure_ fields keep what failed before it.
POST /v1/webhooks/{webhook_id}/deliveries/{delivery_id}/retry queues a delivery again with all five attempts and the original event_id. It answers 204 once the delivery is queued; the outcome shows in the log. A delivery your receiver already accepted is refused with 400.
POST /v1/webhooks/{webhook_id}/test sends webhook.test down the real delivery path, with the same secret, signature, headers and five-second budget, and answers with delivered, response_status and error. It makes one attempt, creates no order, moves no money and never counts towards a switch-off.
Changing the receiver without changing the secret
PATCH /v1/webhooks/{webhook_id} takes url, events or both. A field you leave out stays as it was, and events replaces the whole subscription. The endpoint keeps its id, its signing secret and its delivery history, so moving the receiver or adding one event needs no new secret on your side. An empty body is refused with 400. A disabled endpoint is refused too, with 400 and details.reason set to endpoint_disabled; if its address has changed, name the new one in the email that asks support to switch the endpoint back on.
When polling the order is still the right tool
The 202 answer to POST /v1/orders carries what a poll needs: Location, the order's own address, and Retry-After, the seconds to wait before the first poll and between polls, two at present. In mode A, poll until the send_before field appears or the order ends failed or canceled; an order that fails never gets one. An order closes as soon as its energy is delivered, so completed follows ready almost at once, and a poll every two seconds can miss ready entirely.
Polling is the only option for code that cannot run a receiver: a script on a laptop or a worker inside a private network has no public https address to receive at. A poll also gives an order's current state after events arrived out of sequence or while your endpoint was switched off.
What the sandbox does with webhooks
The sandbox at sandbox.nrg.market makes real deliveries to your URL, signed with your secret, with the same retry schedule and the same delivery log as production. Its endpoints and secrets are separate from production's, so a receiver is registered once on each. POST /v1/sandbox/deposit produces a real deposit.credited, and balance.low fires there too. The three maintenance.* events never arrive from the sandbox, because it accepts Auto-refill rules and never runs them. The test event exercises your receiver's signature check and its answer; the branch that handles maintenance.* runs for the first time on production (what else the sandbox simulates).
We have not measured how long an event takes to leave after an order changes state, and the documentation gives no figure for that delay. One account's deliveries go out one after another, with five seconds allowed per attempt, so a slow receiver of yours delays your other events.
Register a receiver and send it a signed test event before the first live order depends on it: POST /v1/webhooks in the API reference →