[{"data":1,"prerenderedAt":204},["Reactive",2],{"/guides/node/unfetch/receive-a-webhook-event":3,"related:7kxWERB7yJ":122},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"language":10,"library":11,"property":12,"output":13,"related":14,"default":6,"body":17,"_type":117,"_id":118,"_source":119,"_file":120,"_extension":121},"/guides/node/unfetch/receive-a-webhook-event","unfetch",false,"","Receive a webhook event","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. This guides will show you how to receive webhook events in Node and the Unfetch library to call the PDFShift's API.","Node","Unfetch","webhook","pdf",[15,16],"save-your-pdf-online-and-get-back-a-url","save-your-pdf-to-your-amazon-s3-bucket",{"type":18,"children":19,"toc":114},"root",[20,28,33,38,51,56,61,66,77,82,91,96,101,109],{"type":21,"tag":22,"props":23,"children":24},"element","p",{},[25],{"type":26,"value":27},"text","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.",{"type":21,"tag":22,"props":29,"children":30},{},[31],{"type":26,"value":32},"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.",{"type":21,"tag":22,"props":34,"children":35},{},[36],{"type":26,"value":37},"Here's a sample:",{"type":21,"tag":39,"props":40,"children":45},"pre",{"className":41,"code":43,"language":44,"meta":7},[42],"language-javascript","const fetch = require('unfetch')\n\n// You can get an API key at https://pdfshift.io\napi_key = 'sk_xxxxxxxxxxxx'\n\nparams = {\n    source: 'https://www.example.com',\n    webhook: 'https://enyygzdj8jy9b.x.pipedream.net/'\n}\n\nconst response = await fetch('https://api.pdfshift.io/v3/convert/pdf', {\n    method: 'POST',\n    headers: {\n        'Content-Type': 'application/json',\n        'X-API-Key': api_key\n    },\n    body: JSON.stringify(params)\n})\n\n// Handle errors:\nif (!response.ok) {\n    throw new Error(`Request failed with status code ${response.status}: ${response.data}`);\n}\n\nconsole.log('The PDF document was generated and you will receive an event on your webhook once it is done.');\n","javascript",[46],{"type":21,"tag":47,"props":48,"children":49},"code",{"__ignoreMap":7},[50],{"type":26,"value":43},{"type":21,"tag":22,"props":52,"children":53},{},[54],{"type":26,"value":55},"(Please note that the webhook requires a valid API key)",{"type":21,"tag":22,"props":57,"children":58},{},[59],{"type":26,"value":60},"We used Requestbin.com to generate a endpoint that will receive our POST event, but you can set your own endpoint with custom parameters.",{"type":21,"tag":22,"props":62,"children":63},{},[64],{"type":26,"value":65},"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:",{"type":21,"tag":39,"props":67,"children":72},{"className":68,"code":70,"language":71,"meta":7},[69],"language-json","{\n    \"success\":true,\n    \"queued\":true\n}\n","json",[73],{"type":21,"tag":47,"props":74,"children":75},{"__ignoreMap":7},[76],{"type":26,"value":70},{"type":21,"tag":22,"props":78,"children":79},{},[80],{"type":26,"value":81},"Once the conversion is actually done at PDFShift, you will receive a POST request containing the following JSON body:",{"type":21,"tag":39,"props":83,"children":86},{"className":84,"code":85,"language":71,"meta":7},[69],"{\n    \"success\": true,\n    \"url\": \"https://pdfshift.s3.amazonaws.com/d/2/2024-03/39775239d74d4accbabb709800f034d2/8a3ec881-fa69-4059-9c71-0f40c633317e.pdf\",\n    \"filesize\": 34980,\n    \"duration\": 1058,\n    \"response\": {\n        \"status-code\": 200,\n        \"content-length\": 0,\n        \"requests\": 0,\n        \"duration\": 934.5173301696777\n    },\n    \"executed\": \"2024-03-06T09:10:10.981098\",\n    \"pdf_pages\": 1\n}\n",[87],{"type":21,"tag":47,"props":88,"children":89},{"__ignoreMap":7},[90],{"type":26,"value":85},{"type":21,"tag":22,"props":92,"children":93},{},[94],{"type":26,"value":95},"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).",{"type":21,"tag":22,"props":97,"children":98},{},[99],{"type":26,"value":100},"In this case, the POST request will contain the following JSON body:",{"type":21,"tag":39,"props":102,"children":104},{"className":103,"code":7,"language":71,"meta":7},[69],[105],{"type":21,"tag":47,"props":106,"children":107},{"__ignoreMap":7},[108],{"type":26,"value":7},{"type":21,"tag":22,"props":110,"children":111},{},[112],{"type":26,"value":113},"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.",{"title":7,"searchDepth":115,"depth":115,"links":116},2,[],"markdown","content:guides:node:unfetch:receive-a-webhook-event.md","content","guides/node/unfetch/receive-a-webhook-event.md","md",[123,127,131,135,139,143,147,151,155,159,163,167,171,175,179,183,184,188,192,196,200],{"_path":124,"title":125,"language":10,"library":11,"_id":126},"/guides/node/unfetch/accessing-secured-pages","Accessing secured pages","content:guides:node:unfetch:accessing-secured-pages.md",{"_path":128,"title":129,"language":10,"library":11,"_id":130},"/guides/node/unfetch/adding-a-custom-header-or-footer","Adding a custom header or footer","content:guides:node:unfetch:adding-a-custom-header-or-footer.md",{"_path":132,"title":133,"language":10,"library":11,"_id":134},"/guides/node/unfetch/adding-a-text-watermark","Adding a text watermark","content:guides:node:unfetch:adding-a-text-watermark.md",{"_path":136,"title":137,"language":10,"library":11,"_id":138},"/guides/node/unfetch/adding-an-image-watermark","Adding an image watermark","content:guides:node:unfetch:adding-an-image-watermark.md",{"_path":140,"title":141,"language":10,"library":11,"_id":142},"/guides/node/unfetch/avoid-conversion-on-error","Avoid the conversion on error when loading the document","content:guides:node:unfetch:avoid-conversion-on-error.md",{"_path":144,"title":145,"language":10,"library":11,"_id":146},"/guides/node/unfetch/convert-html-to-pdf-from-a-url","Convert an HTML document to PDF from a URL","content:guides:node:unfetch:convert-html-to-pdf-from-a-url.md",{"_path":148,"title":149,"language":10,"library":11,"_id":150},"/guides/node/unfetch/convert-html-to-pdf-from-raw-html","Convert an HTML document to PDF from raw HTML","content:guides:node:unfetch:convert-html-to-pdf-from-raw-html.md",{"_path":152,"title":153,"language":10,"library":11,"_id":154},"/guides/node/unfetch/define-a-time-limit","Define a time limit","content:guides:node:unfetch:define-a-time-limit.md",{"_path":156,"title":157,"language":10,"library":11,"_id":158},"/guides/node/unfetch/exporting-only-a-specific-set-of-pages","Exporting only a specific set of pages","content:guides:node:unfetch:exporting-only-a-specific-set-of-pages.md",{"_path":160,"title":161,"language":10,"library":11,"_id":162},"/guides/node/unfetch/generate-a-document-with-full-height","Generate a document with full height","content:guides:node:unfetch:generate-a-document-with-full-height.md",{"_path":164,"title":165,"language":10,"library":11,"_id":166},"/guides/node/unfetch/loading-css-from-a-string","Loading CSS from a string","content:guides:node:unfetch:loading-css-from-a-string.md",{"_path":168,"title":169,"language":10,"library":11,"_id":170},"/guides/node/unfetch/loading-css-from-a-url","Loading CSS from a URL","content:guides:node:unfetch:loading-css-from-a-url.md",{"_path":172,"title":173,"language":10,"library":11,"_id":174},"/guides/node/unfetch/loading-javascript-from-a-string","Loading Javascript from a string","content:guides:node:unfetch:loading-javascript-from-a-string.md",{"_path":176,"title":177,"language":10,"library":11,"_id":178},"/guides/node/unfetch/loading-javascript-from-a-url","Loading Javascript from a URL","content:guides:node:unfetch:loading-javascript-from-a-url.md",{"_path":180,"title":181,"language":10,"library":11,"_id":182},"/guides/node/unfetch/protecting-the-generated-pdf","Protecting the generated PDF","content:guides:node:unfetch:protecting-the-generated-pdf.md",{"_path":4,"title":8,"language":10,"library":11,"_id":118},{"_path":185,"title":186,"language":10,"library":11,"_id":187},"/guides/node/unfetch/save-your-pdf-online-and-get-back-a-url","Save your PDF online and get back a URL","content:guides:node:unfetch:save-your-pdf-online-and-get-back-a-url.md",{"_path":189,"title":190,"language":10,"library":11,"_id":191},"/guides/node/unfetch/save-your-pdf-to-your-amazon-s3-bucket","Save your PDF to your Amazon S3 Bucket","content:guides:node:unfetch:save-your-pdf-to-your-amazon-s3-bucket.md",{"_path":193,"title":194,"language":10,"library":11,"_id":195},"/guides/node/unfetch/send-custom-http-headers","Send custom HTTP headers","content:guides:node:unfetch:send-custom-http-headers.md",{"_path":197,"title":198,"language":10,"library":11,"_id":199},"/guides/node/unfetch/using-cookies","Using cookies to convert HTML documents to PDF","content:guides:node:unfetch:using-cookies.md",{"_path":201,"title":202,"language":10,"library":11,"_id":203},"/guides/node/unfetch/waiting-for-a-custom-element-to-be-ready","Waiting for a custom element to be ready","content:guides:node:unfetch:waiting-for-a-custom-element-to-be-ready.md",1785426832587]