Webhook endpoints

A webhook endpoint is a web address on your server. ThrustLab sends a message to it when something happens, such as a run finishing. That way your script doesn't have to keep asking. You make an endpoint, say which events you want, and your server receives them.

Your server should check that each message really came from ThrustLab. Webhook verification shows how.

Create an endpoint

python
from thrustlab import Client

client = Client()

endpoint = client.webhook_endpoints.create(
    url="https://example.com/webhooks/thrustlab",
    events=["simulation.completed", "simulation.failed"],
    description="Run results",
)
print(endpoint["id"], endpoint["secret"])

The url must start with https://. events lists the events you want, or ["*"] for all of them. description is optional.

The result holds a secret, which starts with whsec_. Your server uses it to check messages. ThrustLab shows the whole secret only now, and when you rotate it. After that you see a short secret_hint. Store the secret before you do anything else.

The events

The events
EventsWhen they happen
simulation.queued, simulation.running, simulation.completed, simulation.failed, simulation.canceledA simulation changes state.
sweep.queued, sweep.running, sweep.completed, sweep.failed, sweep.canceledA sweep changes state.
flight.running, flight.completed, flight.failed, flight.canceledA flight changes state. A flight has no queued event.
compute_units.low_balanceNever sent. The name is allowed so old code keeps working.
component.submitted, component.approved, component.rejectedA part you sent in changes state.
webhook.testYou ask for a test message.

Any other name is refused with the code unknown_event_type.

Manage endpoints

python
client.webhook_endpoints.list()
client.webhook_endpoints.retrieve("wh_...")
client.webhook_endpoints.update("wh_...", events=["*"])
client.webhook_endpoints.delete("wh_...")

update takes url, events, description and enabled. Anything you leave out stays as it was.

Send a test message

python
client.webhook_endpoints.test(endpoint["id"])

ThrustLab sends one webhook.test event to your address. Use it to check that your server answers and verifies the message.

Rotate the secret

python
new = client.webhook_endpoints.rotate_secret(endpoint["id"])
print(new["secret"])

The old secret stops working at once. There is no overlap, so change the secret on your server right after, and do it when little traffic comes in.

When your server doesn't answer

ThrustLab counts a message as delivered when your server answers with a status in the 200s. Otherwise it tries again a few times. If an endpoint keeps failing, ThrustLab turns it off and emails you. Turn it on again with update(endpoint_id, enabled=True).

To see what happened, list the deliveries of an endpoint:

python
for delivery in client.webhook_endpoints.list_deliveries(endpoint["id"], status="failed"):
    print(delivery["id"], delivery["response_status_code"], delivery["error_message"])

The status is pending, succeeded, failed or permanently_failed. You can also filter by event_type and by created_at_gte, a date and time. To send a failed delivery again, call retry_delivery(endpoint_id, delivery_id).

client.webhook_endpoints.list_events() lists the events on your account, whether or not any endpoint received them.