Synchronous API Requests
The DocSpring API supports both synchronous and asynchronous API requests.
- Synchronous API requests make it much easier to integrate DocSpring with your application. When you make a synchronous API request, the API will wait until the PDF is generated before returning a
201response with the PDF’sdownload_url(infiles.pdf). - Asynchronous requests are useful when generating a PDF takes a long time, or when you want to generate a PDF in the background. When you make an asynchronous request, the API will return a
201response immediately, and you can check the status of the request later.
A synchronous create request returns 201 with the record in its final state, whether the PDF was generated or processing failed. Check the submission’s state: processed when the PDF is ready, or a failure state with an error_message that says why (error, syntax_error, image_download_failed or image_processing_failed). The one exception is running out of credits, which returns a 402 with an insufficient_credits error and the record’s ID (see Credits & Rate Limits).
A synchronous batch returns a 402 as soon as any submission in it is waiting for credits, even if other submissions in the batch were already processed and charged. The error has the batch’s submission_batch_id: get the batch to see each submission’s state, and don’t resubmit the batch: the waiting submissions resume automatically once you add credits.
Only requests that create a submission, batch, combined PDF or template PDF, or add files to a submission, wait. A GET always returns the record straight away, in its current state, including a failure state or insufficient_credits.
All of our API client libraries will use this subdomain by default. (You can always override the host when configuring your client.)
You can force the synchronous subdomain to return a pending response immediately by adding the ?wait=false query parameter to one of the requests that wait.
The wait query parameter has no effect on the async subdomain, and other requests don’t take it: wait on any other request is a 422.
Subdomains and Endpoints
Section titled “Subdomains and Endpoints”- Requests to
sync.api.docspring.comare synchronous by default. (The API waits for the PDF to be processed before returning a response.)- Customers in our EU region should use
sync.api-eu.docspring.com
- Customers in our EU region should use
- Requests to
api.docspring.comare asynchronous by default. (Pending PDF is returned immediately.)- Customers in our EU region should use
api-eu.docspring.com
- Customers in our EU region should use
Synchronous API Endpoints
Section titled “Synchronous API Endpoints”- Create Submission:
POST /api/v2/templates/{template_id}/submissions - Create Submission Batch:
POST /api/v2/submissions/batches - Create Combined PDF:
POST /api/v2/combined_pdfs - Create Template:
POST /api/v2/templatesandPOST /api/v2/templates/upload - Update Template Document:
PUT /api/v2/templates/{template_id}/documentandPUT /api/v2/templates/{template_id}/document/upload - Generate More Files:
POST /api/v2/submissions/{submission_id}/generate_files
A template request waits until the template’s PDF has been processed, and until a document_url has been downloaded (its document.state isn’t pending or downloading). A request for more files waits until they’ve been made (none of the submission’s files has the state pending).
Time limit
Section titled “Time limit”The synchronous subdomain waits up to 95 seconds for your PDF. Most PDFs are ready within a few seconds, so this is only an upper limit.
If the PDF still isn’t ready after 95 seconds, the API returns a 504 response with the error code polling_timeout. The error includes the ID of the submission (or the submission_batch_id, combined_pdf_id or template_id of the record you created), and processing carries on in the background:
{ "error": { "code": "polling_timeout", "message": "Timed out waiting for the submission to be processed. It's still being processed: get it with GET /api/v2/submissions/sub_1234567890abcdef01 until its state isn't pending.", "details": [], "request_id": "req_1234567890abcdef01", "submission_id": "sub_1234567890abcdef01" }}Use the ID to check the submission’s status, or use webhooks to find out when it’s ready.
If some of your PDFs regularly take longer than 95 seconds, add ?wait=false to those requests. You’ll get the pending submission straight away, and can then check its status or use webhooks as described above.
Sending a request again
Section titled “Sending a request again”Every request that creates, changes or deletes something (each POST, PUT and DELETE) takes an Idempotency-Key header: a key of your own, such as a UUID, of up to 255 printable ASCII characters. Send a new key with each new request, and the same key each time you send the same request again, such as after a timeout, a network error or a 5xx. For 24 hours, DocSpring answers a request with a key that your API token has already used with the first request’s response, and an Idempotent-Replayed: true header, instead of processing it again.
- The same key with a different request (another path, query or body) is a
422with the error codeidempotency_key_reused. - A
409with the error codeidempotency_key_in_useand aRetry-Afterheader means the first request is still being processed: send the request again after that many seconds. WithoutRetry-After, the first request was processed but its response is no longer kept, because it was too large or its data has expired. The error has the ID of the record the first request created, when it created one. waitdoesn’t change the request: a request with?wait=falseand the same request without it are the same request.
The API clients take the key as each method’s last argument, or as an option.