# n8n

> Scrape pages, map sites and run batches from n8n workflows and the n8n AI Agent.

**TL;DR**: install the community node `n8n-nodes-megalania`, add a
**Megalania API** credential with your key, and use the **Megalania** node in a
workflow or as a tool of the AI Agent node.

## Install

1. In n8n, open **Settings → Community Nodes** and select **Install**.
2. Enter `n8n-nodes-megalania` and confirm.
3. Create a credential of type **Megalania API**. Paste your key (it starts
   with `meg_sk_`). Leave the two hosts as they are. n8n tests the key with a
   free call.

The test is the same call as this one:

```sh
curl -fsS -H "Authorization: Bearer $MEG_API_KEY" "https://api.megalania.io/v1/batches?limit=1"
```

## Operations

| Resource | Operation | API call | What you get |
|---|---|---|---|
| Page | Scrape | `POST /v1` | `content`, `credits_charged`, `request_id`, `truncated` |
| Site | Map | `POST /v1/map` | the site's URLs |
| Batch | Create | `POST /v1/batches` | the batch, with its `id` |
| Batch | Get | `GET /v1/batches/{id}` | the batch's status and counts |
| Batch | List Items | `GET /v1/batches/{id}/items` | each page, with a `result_url` |

Scrape takes the options of the [JSON envelope](/guides/fetch): response type,
engine, proxy tier, country code, main content only and max credits. Leave
engine and proxy tier unset, so routing picks the cheapest route that works.

**Each page costs at most 128 credits** by default (**Max Credits**). A bigger
page comes back cut, and `truncated` says `max_credits`. Raise it in the node
to get the whole page. Blocked pages and errors are free.

For a crawl, put one URL in **URLs** and a JSON object in **Crawl**, for
example `{"max_pages": 50}`. Then poll **Get** until `status` is `done`, and
read the pages from **List Items**.

The code is MIT licensed at https://github.com/megalania/megalania-typescript.
