# Send Netlify log drains to S3 or your own bucket

A [Netlify log drain](https://docs.netlify.com/manage/monitoring/log-drains/) sends site traffic, function, edge function and deploy logs as they are written. Send2Bucket takes them at its generic JSON endpoint and delivers them to a bucket or table you own.

:::note
We sent NDJSON requests with traffic and function records shaped like the fields Netlify documents to a local Send2Bucket node writing to S3-compatible storage, and ran the check request and the DuckDB queries (DuckDB 2.0) on the objects it wrote. The Netlify setup follows its log drain docs and was not run against Netlify.
:::

## How it works

```text
Netlify ──JSON or NDJSON──▶ /v1/events/json ──▶ your stream's destination
```

- Each log record becomes one line with `signal` `log`, kept as Netlify sent it. Traffic records have `log_type` `traffic`; function and edge function records have `log_type` `functions`. [Delivery format](../delivery-format.md#log-drains) describes the line.
- A `200` means the records are stored durably and will be delivered at least once. See [Delivery semantics](../delivery-semantics.md).
- Log drains need a Netlify Enterprise plan, and a Team Owner sets them up.

## 1. Create a stream and a key

Create an ingestion stream in the [dashboard](pathname:///app/), add a destination, and copy the stream's API key and ID. A key for one stream needs no stream ID in the drain. A stream for Netlify alone keeps the queries below to its records.

## 2. Add the drain

In the site's logs settings, add a log drain of type **General HTTP endpoint**, with the log types you want and the JSON or NDJSON format:

- URL: `https://api.send2bucket.com/v1/events/json?stream=<stream_id>`
- Authorization header: `Bearer <key>`. If the field takes only a token, put the key in the URL instead: `…/v1/events/json?api-key=<key>&stream=<stream_id>`.

Netlify applies drain changes within about five minutes.

## 3. Check the endpoint

Before Netlify sends anything, check the key and stream with one record. It stores one record, and the answer is a `200` with an empty body:

```bash
curl -i "https://api.send2bucket.com/v1/events/json?stream=<stream_id>" \
  -H "Authorization: Bearer $S2B_API_KEY" \
  --data-binary '{"log_type":"traffic","method":"GET","url":"/check","status_code":200,"duration":1}'
```

A `401` means the key is wrong or revoked; a `403` or `400` names the stream problem; a `409` means the stream has no destination. The [API reference](../api-reference.md#log-drains) lists every error.

## 4. Query the logs

[DuckDB](../querying/duckdb.md) reads the objects in place. This view keeps the drained logs:

```sql
INSTALL httpfs; LOAD httpfs;
CREATE SECRET s2b (TYPE s3, PROVIDER credential_chain, REGION '<region>');

CREATE VIEW drains AS
SELECT * FROM read_json(
  's3://<bucket>/<prefix>/s2b/stream=*/date=*/hour=*/*.ndjson.gz',
  format = 'newline_delimited',
  hive_partitioning = true,
  columns = {v: 'INTEGER', delivery_batch_id: 'VARCHAR', batch_id: 'VARCHAR',
             received_at: 'TIMESTAMPTZ', signal: 'VARCHAR',
             resource: 'JSON', scope: 'JSON', data: 'JSON'})
WHERE signal = 'log';
```

Requests per status code, from traffic logs:

```sql
SELECT (data->>'$.status_code')::INTEGER AS status, count(*) AS requests
FROM drains
WHERE data->>'$.log_type' = 'traffic'
GROUP BY ALL
ORDER BY requests DESC;
```

The slowest URLs, by median processing time in milliseconds:

```sql
SELECT data->>'$.url' AS url, count(*) AS requests,
       median((data->>'$.duration')::DOUBLE) AS median_ms
FROM drains
WHERE data->>'$.log_type' = 'traffic'
GROUP BY ALL
ORDER BY median_ms DESC
LIMIT 20;
```

Function errors by function:

```sql
SELECT data->>'$.function_name' AS function, count(*) AS errors
FROM drains
WHERE data->>'$.log_type' = 'functions' AND data->>'$.level' = 'ERROR'
GROUP BY ALL
ORDER BY errors DESC;
```

In an Iceberg or DuckLake table, the same line is in the `payload` column with `signal_type` `log`; see [Table columns](../delivery-format.md#table-columns).

## Notes

- **Billing**: each record's JSON as stored is the number billed. See [Plans and billing](../plans-and-billing.md).
- **Retries**: see [Retries](../api-reference.md#retries). To drop repeats, key on `request_id` with the record's `timestamp`.
- **Keys in URLs**: a key in the URL can end up in the platform's logs, so prefer the authorization header. See [Drain keys and streams](../api-reference.md#drain-keys).
- **Other platforms**: [Log drains](./log-drains.md) covers Vercel, Heroku, Supabase, Cloudflare, Fastly and others.
