Articles

Use Python libraries in n8n Cloud with a FastAPI endpoint

Run pypdf behind an authenticated FastAPI endpoint, pass a binary PDF from an n8n Cloud workflow, and verify the page order.

Updated

n8n Cloud's Python Code node does not let you import Python packages, including packages from the standard library. If an operation needs a library such as pypdf, leave orchestration in n8n and call a small Python service with an HTTP Request node. Self-hosted n8n is different: its task runners can be configured with additional dependencies. If you just need to extract PDF text, check n8n's built-in Extract From File node first.

Get the runnable example on GitHub.

This example reverses the pages in a two-page synthetic PDF. It does not fetch arbitrary URLs or need a database. Use test files you control, not confidential documents. The service accepts at most 1 MiB and 12 pages per request; these bounds are for this demo, not a promise about arbitrary PDF parsing or a replacement for further controls on an untrusted public upload service.

Quick start

  1. Keep your workflow in n8n Cloud. Download the example bundle, then deploy the demo/ directory as a separate FastAPI app. Use the Small preset (256 MB), which passed on our Scale test account; 128 MB did not start. We have not verified a fresh Free account.
  2. Run the smoke test against its HTTPS URL. The first page of the response must read SECOND PAGE and the second FIRST PAGE.
  3. Import the workflow, set the two URLs and a Header Auth credential, and run it manually with the bundled synthetic PDF.

What we verified

The local smoke test and requests to the Gregale sample verified that POST /pdf/reverse returns an actual reversed PDF, not just a 200 response. The local smoke test also checked rejection without the app key (401), oversized input (413), the wrong media type (415), and an invalid PDF (422). We did not execute the imported workflow in hosted n8n Cloud. Inspect the two-page PDF returned by the local smoke run.

1. Deploy the PDF service

Download the example bundle. Unzip it, then use the demo directory as the Gregale source root. It contains main.py, requirements.txt, gregale.yaml, and sample.pdf; keep them together. Install and sign in to the Gregale CLI, then run these commands from demo/. Pick a new app name rather than targeting one you already use.

bash
APP=my-n8n-pdf-demo
PDF_API_KEY="$(python3 -c 'import secrets; print(secrets.token_urlsafe(32))')"
export PDF_API_KEY
(
  umask 077
  secret_file="$(mktemp)"
  trap 'rm -f "$secret_file"' EXIT
  printf 'PDF_API_KEY=%s\n' "$PDF_API_KEY" > "$secret_file"
  gregale deploy --name "$APP" --size small --secrets-file "$secret_file"
)
gregale app update "$APP" --visibility public --public-auth open --no-require-authn

Use the HTTPS URL returned by the deploy. The small preset allocates 256 MB. The 128 MB preset failed the container startup in our test. The service is network-reachable so n8n Cloud can call it, but POST /pdf/reverse requires the app-level X-API-Key. This is the key you just generated, not the Gregale account token. Store it in your password manager and in an n8n credential, not in exported workflows, logs, or source.

2. Verify a real transformation

Set BASE to the app's exact origin, without a trailing slash. The bundled smoke test checks the published route and the PDF contents rather than relying on a health-check response.

bash
BASE=https://replace-with-your-gregale-app-host
python3 -m venv /tmp/gregale-n8n-check
/tmp/gregale-n8n-check/bin/pip install -r requirements.txt
/tmp/gregale-n8n-check/bin/python ../smoke.py "$BASE"

The original page labels must be FIRST PAGE then SECOND PAGE; the response must be SECOND PAGE then FIRST PAGE. The script also checks for 401 without a valid credential, 413 for oversize input, 415 for the wrong media type, and 422 for non-PDF input.

3. Import the n8n workflow

  1. In the n8n workflow editor, use Import from File to import workflow.json from the bundle.
  2. In Download synthetic PDF, replace the example URL with your app's /sample.pdf. Keep Response Format: File, with output field pdf.
  3. In Reverse PDF on Gregale, replace the example URL with your app's /pdf/reverse. Under Generic Credential Type → Header Auth, create and select a credential named for this demo, with header name X-API-Key and the generated app key as its value. Keep Content-Type: application/pdf, Body Content Type: n8n Binary File, input field pdf, and Response Format: File with output field reversed. The request sends raw PDF bytes, not multipart or JSON.
  4. Execute the workflow manually. In the last node's Binary output, download the file in reversed. Open it and check that page 1 reads SECOND PAGE and page 2 reads FIRST PAGE.

To use a file from another node, connect that node directly to the POST step and replace pdf with the actual name of its binary property. Keep the workflow manual until you have checked both a good input and a failed run. For any sensitive files, review n8n execution-data retention, your own service logging, and your organization's data handling requirements before using this design.

If the workflow fails

What you seeWhat to check
n8n reports no binary propertyThe first node must emit a file under pdf; the POST must use the same field name.
401Select the Header Auth credential and check that it holds the app key, not the Gregale CLI token.
413 or 415Use raw PDF bytes under 1 MiB and set Content-Type: application/pdf.
422Try the bundled sample. Encrypted, malformed, or more-than-12-page inputs are outside this example.
503 saying the key is missingConfirm PDF_API_KEY was set on the app you deployed.

For node settings, see n8n's HTTP Request documentation. For a different Python package, keep the same HTTP boundary, pin the package, and choose an input and output contract your workflow can verify.

If the work can outlast the incoming webhook, send a job receipt instead of waiting for the PDF. See Fix n8n Cloud webhook timeout (524).

Questions

Can I import pypdf inside the n8n Cloud Python Code node?

No. Deploy the Python operation as an HTTPS service and call it with an HTTP Request node.

Which API key goes in n8n?

Use the dedicated demo app key in an n8n Header Auth credential, not a Gregale account token.