Beta API. See the stability policy.

Webhooks

A webhook lets ThrustLab tell your server when something happens, so you don't have to keep checking. When a simulation finishes, ThrustLab sends a request to an address you chose. This article shows how to set one up, how to check that a request really came from ThrustLab, and what happens when your server doesn't answer.

Set up a webhook

Send a POST to /v1/webhook_endpoints with the address and the events you want. The address must start with https and be reachable on the public internet. The events are listed in Event types. To get all of them, use ["*"]. A webhook can name the flight events too, such as flight.completed and flight.failed.

curl -X POST https://thrustlab.com/v1/webhook_endpoints \
-H "Authorization: Bearer key_..." \
-H "Content-Type: application/json" \
-d '{
  "url": "https://yourapp.example.com/hooks/thrustlab",
  "events": ["simulation.completed", "simulation.failed"],
  "description": "Prod ingest"
}'

The reply contains a secret that starts with whsec_. You see it only once, so save it right away. After that, the endpoint shows only the last four characters. If you lose the secret, ask for a new one with POST /v1/webhook_endpoints/{id}/rotate_secret. The old one stops working at once.

A new webhook only gets events from after you made it. To get older events, read them with GET /v1/events.

To change the events later, send a PATCH to the endpoint with a new events list.

Check that a request is from ThrustLab

Anyone could send a request to your address, so check each one. Every request from ThrustLab has a Thrustlab-Signature header. It looks like t=1714070000,v1=3f9a.... The part after v1= is a code made from your secret, the time and the body of the request.

To check it, build the text <t>.<body>, which is the time, a dot, and the body as it arrived. Make a code from it with HMAC-SHA256 and your secret. It must match the v1 part. Use the body as it came off the wire. If you parse the JSON and write it out again, the spacing changes and the check fails. That's the most common mistake.

The Python library does all of this for you. It also rejects a request that is more than 5 minutes old, so nobody can send an old one again.

python
from fastapi import FastAPI, Request, HTTPException
from thrustlab import Webhook
from thrustlab.exceptions import SignatureVerificationError

app = FastAPI()
SECRET = "whsec_..."  # read it from an environment variable in real code


@app.post("/webhooks/thrustlab")
async def thrustlab_webhook(request: Request):
    body = await request.body()
    signature = request.headers.get("Thrustlab-Signature", "")
    try:
        event = Webhook.verify(body, signature, SECRET)
    except SignatureVerificationError as exc:
        raise HTTPException(status_code=400, detail=str(exc))

    if event.type == "simulation.completed":
        print("simulation finished:", event.data["object"]["id"])

    return {"received": True}

The same event can arrive twice

ThrustLab sends each event at least once. If your answer gets lost on the way, it sends the event again. Each request has a Webhook-Id header. Save the IDs you've handled, and skip any you've seen. A Webhook-Attempt header says which try this is, starting at 1.

What happens if your server fails

Answer with any 2xx status as soon as you have the event. The body doesn't matter. ThrustLab waits up to 15 seconds for your answer. If it gets a 5xx status, a 429 or no answer at all, it tries again, up to 5 tries in all:

What happens if your server fails
TryWhen
1Right away.
21 minute after try 1 fails.
330 minutes after try 2 fails.
42 hours after try 3 fails.
524 hours after try 4 fails.

After the fifth try fails, the delivery is marked permanently_failed. A 404 or 410 answer stops it at once, because that says the address is gone.

If an endpoint has no successful delivery for 7 days while deliveries are failing, ThrustLab turns it off. It stops sending to it and sets auto_disabled_at. To turn it on again, send a PATCH with {"enabled": true}.

Try it with a test event

POST /v1/webhook_endpoints/{id}/test sends one webhook.test event to that endpoint only. Use it to check your signature code.

curl -X POST https://thrustlab.com/v1/webhook_endpoints/wh_.../test \
-H "Authorization: Bearer key_..."

See what was sent

Every delivery is kept so you can see what happened.

  • GET /v1/webhook_endpoints/{id}/deliveries lists the deliveries of one endpoint, newest first. You can filter by status (pending, succeeded or permanently_failed) and by event_type.
  • GET /v1/webhook_endpoints/{id}/deliveries/{delivery_id} shows one in full. You get the body that was sent, the status code of your answer, the first part of your answer, any error, and the time of the last try and the next one.

To send a finished delivery again, use POST /v1/webhook_endpoints/{id}/deliveries/{delivery_id}/retry. It makes a new delivery with the original body. A delivery that is still pending can't be sent again. You get a 409.