A complete Flask app that sends a contract for signature, lets the user sign it inside your page (embedded ceremony), and tracks the result with verified webhooks.
Most e-signature integrations need three things working together: a server call that creates the envelope, a signing experience that keeps users in your product, and a reliable way to learn when signing is done. Getting any one wrong leaks your API key, breaks the iframe, or trusts forged webhook calls.
A small Flask app (app.py, about 90 lines; the API client uses only the standard library):
| Route | Purpose |
|---|---|
GET / |
A page with a form and the ceremony iframe |
POST /api/contracts |
Creates the envelope on the server and returns the ceremony URL |
GET /api/contracts/{id} |
Confirms the envelope status on the server |
GET /api/contracts/{id}/signed.pdf |
Streams the signed PDF (the deliverable) through your server |
POST /webhooks/signatureapi |
Receives events, verifies the signature on the raw body, dedupes retries |
signatureapi.py holds the API client, the envelope builder and the Standard Webhooks verifier.
- Python 3.10 or newer.
- A SignatureAPI test API key (
key_test_...). Sign up for free. - For webhooks on your machine: a webhook endpoint in the dashboard pointing at a public tunnel, or
npx signatureapi listenfrom the SignatureAPI CLI, which forwards test events and saves the signing secret.
The browser never sees your API key. POST /api/contracts calls build_contract(), which sets three recipient options for embedding:
"recipients": [{
"key": "client", "type": "signer", "name": name, "email": email,
"delivery_type": "none", # your app shows the result; no emails
"ceremony": {
"authentication": [{"type": "custom", "provider": "Acme Corp Portal", "data": {"user_id": user_id}}],
"embeddable_in": [app_origin], # the origin allowed to frame the ceremony
},
}],custom authentication records that your app already signed the user in, so the ceremony URL comes back in the response at recipients[0].ceremony.url.
The envelope starts in processing, and the ceremony opens only once it is in_progress. create_signing_session() reads the envelope once a second until then and returns the ceremony URL from that read.
templates/index.html adds the two required query parameters and loads the URL in an iframe:
src.searchParams.set('embedded', 'true');
src.searchParams.set('event_delivery', 'message');It accepts message events only when event.origin is https://sign.signatureapi.com and event.source is the iframe. A ceremony.completed message is a UI signal, so the page then asks your server for the real status.
SignatureAPI signs each delivery with the Standard Webhooks scheme. verify_webhook() computes an HMAC-SHA256 over webhook-id.webhook-timestamp.body with your signing secret, compares in constant time, and rejects timestamps older than 5 minutes. It runs on request.get_data(), the exact bytes received: parsing and re-serializing the JSON first changes the bytes and breaks the signature.
Deliveries retry for up to 48 hours and can arrive out of order. The handler remembers each webhook-id and answers duplicates with 200 without processing them again. In production, store the ids in your database with a unique index.
When the envelope completes, SignatureAPI generates the deliverable: the signed PDF with its audit log. download_signed_pdf() reads the envelope, gets its deliverable, and downloads the file from the deliverable's url. That URL is pre-signed and expires after 1 hour, so the app requests a fresh one on each download instead of storing it. Until the deliverable is generated, the route answers 409 and the page can try again a few seconds later.
python3 -m venv .venv && . .venv/bin/activate
pip install -r requirements.txt
export SIGNATUREAPI_KEY=key_test_YOUR_KEY
export SIGNATUREAPI_WEBHOOK_SECRET=whsec_... # from the webhook endpoint in the dashboard
export APP_ORIGIN=http://localhost:5000
flask --app app runOpen http://localhost:5000, keep the sample name and email, and select Start signing.
* Running on http://127.0.0.1:5000
127.0.0.1 - - "POST /api/contracts HTTP/1.1" 201 -
127.0.0.1 - - "POST /webhooks/signatureapi HTTP/1.1" 200 -
127.0.0.1 - - "GET /api/contracts/eb4cf433-a8af-4161-b37b-7ef57d423e56 HTTP/1.1" 200 -
After signing, the page shows Thanks! Envelope status: completed.
python3 -m unittest discover -s tests -t .The tests run the shared webhook test vector through the verifier, then drive the app with Flask's test client against a local mock API and check the envelope payload, the 422 path, webhook acceptance, tamper rejection and duplicate handling. With SIGNATUREAPI_KEY set, a live test creates a real embedded-ceremony envelope in test mode.
- The iframe is blank or refuses to load:
embeddable_inmust match your page's origin exactly (scheme, host and port), and the URL needsembedded=true. - No
messageevents arrive: addevent_delivery=messageto the URL, and check that your origin filter useshttps://sign.signatureapi.com. - Every webhook returns 400
Invalid webhook signature: wrong secret (each endpoint has its own), or a proxy or body parser changed the body before verification. Webhook timestamp outside the tolerance window: your server clock is off. Sync it with NTP.ceremonyUrlis null: the recipient is not usingcustomauthentication. Withemail_link, SignatureAPI emails the link instead.