# Send the PDF report to Google Drive

> With this recipe, every case closed in Certificall automatically uploads its **signed PDF report** to the Google Drive folder of your choice, through a four-node n8n workflow.

# Automatically send the PDF report to Google Drive with n8n

It produces the same result as the [Zapier recipe](../zapier/rapport-pdf-google-drive.md), with three advantages: the webhook is protected by a **secret checked on every call**, the download and upload are **retried automatically** on failure, and you can **host n8n yourself**, in the European Union or in your own infrastructure. See [Zapier or n8n?](../introduction.md#zapier-or-n8n)

:::info Before you start
How it works, the common prerequisites (including **case sharing**), the webhook content and the common limitations are described in [Automate without coding](../introduction.md). Read it first.
:::

## n8n-specific prerequisites

| Prerequisite | Details |
|---|---|
| **An n8n instance** | On the n8n service, or self-hosted. It must be reachable from the internet over HTTPS, with a domain name. |
| **A Google Drive account** | And the folder in which to store the reports. **It must exist before** you configure the workflow. |
| **A secret** | A long random value of your choice. It will be registered both in n8n and with Certificall. |

## Go faster: let the AI build the workflow

n8n can build the workflow for you from a description. Copy this prompt, paste it into the workflow editor's AI assistant, then check the result. You can also [import the ready-made workflow](#import-the-workflow).

{`Create a 4-node n8n workflow:

1. Webhook
   - Method: POST.
   - Path: certificall-case-closed.
   - Authentication: Header Auth. I will create the credential myself (Name: Authorization, Value: Bearer followed by my secret).
   - Respond: Immediately.

2. IF: continue only if both conditions are true (AND).
   - {{ $json.body.closed }}: Boolean, is true.
   - {{ $json.body.caseUrl }}: String, is not empty.
   Only the true output is connected to the next node.

3. HTTP Request, named "Download PDF"
   - Method: GET. URL: {{ $json.body.caseUrl }}. No authentication.
   - Response Format: File, in the binary field "data".
   - Settings: Retry On Fail, 3 tries, 5000 ms between tries.

4. Google Drive, operation "Upload file"
   - Input Data Field Name: data.
   - File Name: Certificall_{{ $('Webhook').item.json.body.cfRef }}.pdf
   - Settings: Retry On Fail, 3 tries.
   I will choose the Google account and the destination folder myself.

Do not put any secret or credential in the workflow.`}

Once the workflow is created, you still need to:

1. **Check each node** against the settings described below: the AI may deviate slightly from the request.
2. **Create the Header Auth credential** with your secret (node 1).
3. **Connect your Google account** and choose the destination folder (node 4).
4. **Have the production URL and the secret registered** with Certificall (node 1, item 4).
5. **Test**, then **activate** the workflow.

:::info No AI assistant in your version?
The availability of the AI assistant depends on your n8n plan and version. If you do not have access to it, import the workflow or follow the steps below.
:::

## Workflow overview

```mermaid
flowchart LR
    W["Webhook: secret checked"] --> I{"IF: closed and caseUrl present?"}
    I -- yes --> H["HTTP Request: downloads the PDF"]
    H --> G["Google Drive: uploads the PDF"]
    I -- no --> S((end))
```

## Node 1 — Webhook: receive the closed case

1. Create a new workflow, then add a **Webhook** node.
2. Configure it as follows:

| Setting | Value |
|---|---|
| **HTTP Method** | `POST` |
| **Path** | A path of your choice, for example `certificall-case-closed` |
| **Authentication** | **Header Auth** |
| **Respond** | **Immediately** (immediate `200` response) |

3. For **Header Auth**, create a credential with:

| Field | Value |
|---|---|
| **Name** | `Authorization` |
| **Value** | `Bearer <your-secret>` |

:::warning The word "Bearer" is part of the value
Type `Bearer`, **a space**, then your secret. This is the exact format of the header sent by Certificall. Without this prefix, n8n rejects every call.
:::

4. Have the webhook's **production URL**, of the form `https://<your-n8n-instance>/webhook/certificall-case-closed`, and **your secret** as the token registered with Certificall:
   - **by your technical team**, through the API: see [Webhook configuration](../../api/webhook.md#configuration). The address goes in `endpoint`, the secret in `authToken`, without the word `Bearer`;
   - **or by your Certificall contact**, to whom you send the address and the secret through a secure channel.

:::caution Production URL, not test URL
n8n displays two addresses. The test URL (`/webhook-test/…`) only responds while the editor is listening for a call. Register the **production URL** (`/webhook/…`) with Certificall.
:::

In n8n, the webhook content arrives under `body`: the case reference is written, for example, `{{ $json.body.cfRef }}`.

## Node 2 — IF: only process usable cases

Add an **IF** node, which only lets through closed cases that have a download link. Both conditions must be true (**AND**):

| Value tested | Condition |
|---|---|
| `{{ $json.body.closed }}` | **Boolean** → **is true** |
| `{{ $json.body.caseUrl }}` | **String** → **is not empty** |

Only the **true** output is connected to the rest of the workflow.

## Node 3 — HTTP Request: download the PDF report

Add an **HTTP Request** node, which you can rename "Download PDF":

| Setting | Value |
|---|---|
| **Method** | `GET` |
| **URL** | `{{ $json.body.caseUrl }}` |
| **Authentication** | None |
| **Response Format** (in *Options* → *Response*) | **File** |
| **Put Output in Field** | `data` |

In the node's **Settings** tab, enable **Retry On Fail**, with **3** tries (*Max Tries*) and a **5,000 ms** interval (*Wait Between Tries*).

## Node 4 — Google Drive: upload the PDF

Add a **Google Drive** node, with the **Upload file** operation, then connect your Google account:

| Setting | Value |
|---|---|
| **Input Data Field Name** | `data` |
| **File Name** | `Certificall_{{ $('Webhook').item.json.body.cfRef }}.pdf` |
| **Parent Drive** and **Parent Folder** | The destination folder, which must already exist |

In the node's **Settings** tab, enable **Retry On Fail**, with **3** tries.

:::warning Don't forget the .pdf extension
Type `.pdf` at the end of the file name. Without it, Google Drive does not open the file as a PDF.
:::

The file name fetches `cfRef` from the **Webhook** node: at this point in the workflow, the current item is the downloaded PDF, no longer the webhook content.

## Activate the workflow

Close a test case to check that the PDF arrives in your Drive, then **activate the workflow** (*Active* toggle at the top of the editor).

:::warning Nothing happens until it is activated
As long as the workflow is not activated, the production URL does not respond, and the webhooks sent by Certificall are lost.
:::

### Optional: organize your reports better

- **Name the file with your own reference**: use `{{ $('Webhook').item.json.body.reportId }}` instead of or in addition to `cfRef`.
- **Include the template in the name**: add `{{ $('Webhook').item.json.body.frame }}`, which contains the template name.

## Import the workflow

Instead of creating the nodes by hand, you can import this workflow into n8n in two ways:

- **By copy and paste**: copy the JSON below, open an empty workflow, click on the canvas, then paste (Ctrl+V, or Cmd+V on a Mac). The four nodes appear.
- **From a file**: save the JSON to a `.json` file, then, in an empty workflow, open the **⋯** menu at the top right of the editor and choose **Import from File**.

Then fill in the Header Auth and Google Drive credentials, and choose your destination folder.

```json
{
  "name": "Certificall - PDF report to Google Drive",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "certificall-case-closed",
        "authentication": "headerAuth",
        "responseMode": "onReceived",
        "options": {}
      },
      "id": "webhook",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [0, 0],
      "webhookId": "certificall-case-closed"
    },
    {
      "parameters": {
        "conditions": {
          "options": { "caseSensitive": true, "leftValue": "", "typeValidation": "strict" },
          "conditions": [
            {
              "id": "closed-is-true",
              "leftValue": "={{ $json.body.closed }}",
              "rightValue": "",
              "operator": { "type": "boolean", "operation": "true", "singleValue": true }
            },
            {
              "id": "caseurl-not-empty",
              "leftValue": "={{ $json.body.caseUrl }}",
              "rightValue": "",
              "operator": { "type": "string", "operation": "notEmpty", "singleValue": true }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if",
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [220, 0]
    },
    {
      "parameters": {
        "url": "={{ $json.body.caseUrl }}",
        "options": {
          "response": {
            "response": { "responseFormat": "file", "outputPropertyName": "data" }
          }
        }
      },
      "id": "download-pdf",
      "name": "Download PDF",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, -100],
      "retryOnFail": true,
      "maxTries": 3,
      "waitBetweenTries": 5000
    },
    {
      "parameters": {
        "name": "=Certificall_{{ $('Webhook').item.json.body.cfRef }}.pdf",
        "driveId": { "__rl": true, "mode": "list", "value": "My Drive" },
        "folderId": { "__rl": true, "mode": "id", "value": "<your-drive-folder-id>" },
        "inputDataFieldName": "data",
        "options": {}
      },
      "id": "upload-file",
      "name": "Upload file",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [660, -100],
      "retryOnFail": true,
      "maxTries": 3
    }
  ],
  "connections": {
    "Webhook": { "main": [[{ "node": "IF", "type": "main", "index": 0 }]] },
    "IF": { "main": [[{ "node": "Download PDF", "type": "main", "index": 0 }], []] },
    "Download PDF": { "main": [[{ "node": "Upload file", "type": "main", "index": 0 }]] }
  },
  "settings": {}
}
```

## n8n-specific limitations

In addition to the [common limitations](../introduction.md#common-limitations):

| Limitation | Details |
|---|---|
| **No alert by default** | A failure is only visible in the n8n **Executions** tab. To be notified, configure an **Error Workflow** in the workflow settings (*Settings* → *Error Workflow*), for example to receive an email. |
| **Retries do not recover a lost webhook** | Retries apply to the download and the upload to Drive. If n8n does not receive the webhook, for example because it is unavailable or the workflow is not activated, Certificall does not resend it. |
| **Sharing link required** | As with Zapier, the download is done without authentication: it requires sharing to be enabled for the case. See [Case sharing](../introduction.md#common-prerequisites). |

## Troubleshooting

| Symptom | Likely cause | What to do |
|---|---|---|
| Certificall receives a **401** or **403** error from the webhook | The Header Auth value does not match the header sent | Check that the value is exactly `Bearer <your-secret>`, with a space, and that the secret is the same as the one registered with Certificall. |
| Nothing happens when a case is closed | The workflow is not activated, or the URL registered with Certificall is the test URL (`/webhook-test/…`) | Activate the workflow, and have the production URL (`/webhook/…`) registered. |
| The execution stops at the **IF** node | The **caseUrl** field is empty: sharing is not enabled for the case | Contact your Certificall contact to enable case sharing. |
| The **HTTP Request** node fails with a **403** error | The case cannot be shared, or its sharing has expired | Check the case sharing with your Certificall contact. |
| The **HTTP Request** node fails with a **404** error | The link is invalid or the report cannot be found | Check that the node URL is exactly `{{ $json.body.caseUrl }}`, with no added text. |
| The file uploaded to Drive is unreadable | The `.pdf` extension is missing from the file name | Add `.pdf` at the end of the Google Drive node's **File Name**. |
