View all our articles

Receive a webhook event in Ruby with RestClient

In this guide, we'll show you how you can receive a notification via a POST request on your server once the conversion was done. This allows you to trigger a massive amount of conversion without having to wait on their results, and get notified once they are done.

For this guide, we are going to use the "webhook" parameter when sending a request to PDFShift. It expects a URL to which the POST request will be sent once the conversion is done.

Here's a sample:

require 'rest-client'
require 'json'

# You can get an API key at
api_key = 'sk_xxxxxxxxxxxx'

params = {
    'source' => '',
    'webhook' => ''

# Make the POST request
response ="", params.to_json, {
    'Content-Type' => 'application/json',
    'Authorization' => "Basic #{Base64.strict_encode64("api:#{api_key}")}"

# Check for successful response
unless response.code == 200
    raise "Request failed with status code #{response.code}: #{response.body}"

# Print a success message
puts 'The PDF document was generated and you will receive an event on your webhook once it is done.'

(Please note that the webhook requires a valid API key)

We used to generate a endpoint that will receive our POST event, but you can set your own endpoint with custom parameters.

When sending the above request to PDFShift, the response will be immediate with a status code of 202 (Request accepted), with the following JSON body:


Once the conversion is actually done at PDFShift, you will receive a POST request containing the following JSON body:

    "success": true,
    "url": "",
    "filesize": 34980,
    "duration": 1058,
    "response": {
        "status-code": 200,
        "content-length": 0,
        "requests": 0,
        "duration": 934.5173301696777
    "executed": "2024-03-06T09:10:10.981098",
    "pdf_pages": 1

It might occur that the conversion fails, for instance if PDFShift can't access the page or if there is any parameter that cause the loading page to fail (page taking to long to load, etc).

In this case, the POST request will contain the following JSON body:

This can be useful if you don't need to wait for the conversion to continue your process. For instance, when generating all the invoices from last month's payment, you can trigger the conversions and save the PDF on your webhook's destination. This way, you can continue your process without having to wait for the conversion to be done.

For further details on the webhook property and its usage, please refer to our dedicated documentation.

We hope this guide was helpful. If you have any questions or noticed any issues on the code above,
feel free to drop us a line.