---
name: tinybird-0d23501618fd
description: >-
  Build real-time analytics APIs with Tinybird — ingest millions of events and
  query with SQL over HTTP. Use when someone asks to "build analytics API",
  "Tinybird", "real-time analytics", "event analytics", "ClickHouse as a
  service", "usage metering", or "product analytics backend". Covers data
  ingestion, SQL pipes, API endpoints, and real-time dashboards.
license: Apache-2.0
compatibility: "Tinybird account (Free plan works). Tinybird CLI installs with pip; Tinybird Local needs Docker. HTTP API from any language."
metadata:
  author: terminal-skills
  version: "1.1.0"
  category: data-ai
  tags: "analytics, tinybird, real-time, clickhouse, events"
---

# Tinybird

## Overview

Tinybird is managed ClickHouse with an API layer: you send events over HTTP, write SQL in Pipes, and publish a Pipe as an authenticated HTTP Endpoint that returns JSON, CSV, NDJSON or Parquet. It suits product analytics, usage metering for billing, live dashboards and any aggregation over large append-only event tables.

The current product is Tinybird Forward. Projects are files in git (`.datasource`, `.pipe`, `.connection`), built in Tinybird Local or a Cloud Branch with `tb build` and shipped with `tb deploy`. The old Classic commands (`tb auth`, `tb push`, the `tinybird-cli` package) are not the workflow described here. TypeScript (`@tinybirdco/sdk`) and Python (`tinybird-sdk`) SDKs can define the same resources in code; this skill uses datafiles.

## Instructions

### Install and log in

```bash
pip install tinybird          # installs the Forward CLI as `tb` (5.x)
tb --version
mkdir product-analytics && cd product-analytics && git init
tb init --type cli --dev-mode local --folder tinybird --cicd skip
tb login                      # opens a browser; writes .tinyb (never commit it)
```

Tinybird also publishes an installer script at tinybird.co; read it before running it instead of piping it into a shell. If a Classic `tb` is already on the PATH (`which tb`), remove it or use a separate virtualenv, because both CLIs use the same command name.

`tb init` creates `tinybird.config.json`, `.env.local` and the folders `datasources/`, `pipes/`, `endpoints/`, `materializations/`, `copies/`, `sinks/`, `connections/`, `fixtures/`, `tests/`. Without `--cicd skip` it asks about CI/CD templates. `dev_mode` is `local`, `branch` or `manual`.

### Define a Data Source

```text
# tinybird/datasources/events.datasource
DESCRIPTION >
    Raw product events sent from the app

SCHEMA >
    `event_id` String `json:$.event_id`,
    `user_id` String `json:$.user_id`,
    `event_type` LowCardinality(String) `json:$.event_type`,
    `properties` String `json:$.properties`,
    `timestamp` DateTime `json:$.timestamp`

ENGINE "MergeTree"
ENGINE_PARTITION_KEY "toYYYYMM(timestamp)"
ENGINE_SORTING_KEY "event_type, timestamp, user_id"
ENGINE_TTL "timestamp + toIntervalDay(400)"

TOKEN events_append APPEND
```

Rules that matter: the sorting key decides query speed (put the columns you filter on first); partition monthly or yearly only, because fine-grained partitions slow writes; `TOKEN <name> APPEND` creates a token that can only write to this table. Changing a deployed schema in an incompatible way needs a `FORWARD_QUERY`.

### Ingest events

`POST /v0/events?name=events` takes NDJSON (one JSON object per line). Batch several events per request: it is much faster than one request per event. Use the host of your workspace region (`api.tinybird.co` for the default region).

```typescript
// src/analytics/track.ts
const TINYBIRD_HOST = process.env.TINYBIRD_HOST ?? "https://api.tinybird.co";
const INGEST_TOKEN = process.env.TINYBIRD_INGEST_TOKEN!; // the events_append token

type AppEvent = { userId: string; eventType: string; properties?: Record<string, unknown> };

export async function trackEvents(events: AppEvent[]) {
  const now = new Date().toISOString().slice(0, 19).replace("T", " "); // 2026-10-24 17:05:09
  const body = events
    .map((e) =>
      JSON.stringify({
        event_id: crypto.randomUUID(),
        user_id: e.userId,
        event_type: e.eventType,
        properties: JSON.stringify(e.properties ?? {}),
        timestamp: now,
      })
    )
    .join("\n");

  const res = await fetch(`${TINYBIRD_HOST}/v0/events?name=events`, {
    method: "POST",
    headers: { Authorization: `Bearer ${INGEST_TOKEN}` },
    body,
  });
  if (!res.ok) throw new Error(`Tinybird ingest failed: ${res.status} ${await res.text()}`);
  return res.json(); // { successful_rows: 2, quarantined_rows: 0 }
}

await trackEvents([
  { userId: "usr_8f3a21", eventType: "page_view", properties: { page: "/pricing" } },
  { userId: "usr_8f3a21", eventType: "button_click", properties: { button: "start-trial" } },
]);
```

Responses: `202` means accepted but not yet committed; add `&wait=true` for a `200` after the write is acknowledged (slower, use a timeout of 10 seconds or more). `400` and `403` must not be retried; `413` means the body is too big (10 MB on Free, 100 MB on paid plans); `429` means over the default 100 requests per second per Data Source, so back off with `Retry-After`. The API is not idempotent: a retried batch can insert duplicates. Check `quarantined_rows` in the answer: rows that do not match the schema land in the `events_quarantine` table instead of failing the request.

### Pipes that become Endpoints

A Pipe is an Endpoint only when it ends with `TYPE endpoint`. Start the SQL block with `%` so the parameters are parsed.

```text
# tinybird/endpoints/daily_active_users.pipe
TOKEN dashboard_read READ

DESCRIPTION >
    Daily active users for the last N days

NODE daily_counts
SQL >
    %
    SELECT toDate(timestamp) AS date, uniqExact(user_id) AS active_users
    FROM events
    WHERE timestamp >= now() - INTERVAL {{ Int32(days, 30) }} DAY
    GROUP BY date
    ORDER BY date DESC

TYPE endpoint
```

```text
# tinybird/endpoints/user_activity.pipe
TOKEN dashboard_read READ

NODE activity
SQL >
    %
    SELECT event_type, count() AS event_count, max(timestamp) AS last_seen
    FROM events
    WHERE user_id = {{ String(user_id, required=True) }}
      AND timestamp >= now() - INTERVAL {{ Int32(days, 7) }} DAY
    GROUP BY event_type
    ORDER BY event_count DESC

TYPE endpoint
```

Parameters are typed (`String`, `Int32`, `DateTime`, `Date`, `Boolean`, `Array`, `column()` for column names), so values are validated and rendered as safe SQL literals. Never build SQL by string concatenation. Optional filters use `{% if defined(country) %} ... {% end %}`. Query parameters cannot be used in Materialized Views.

### Build, test, deploy

```bash
tb local start --daemon            # Tinybird Local: needs Docker, serves http://localhost:7181
tb build                           # validate and create resources in Local (or a branch)
tb --local sql "SELECT count() FROM events"
tb --cloud deploy --check          # dry-run against Cloud
tb --cloud deploy                  # ship to production
tb endpoint ls                     # list endpoints, URLs, tokens
```

`tb dev` rebuilds on every file change. `tb deploy` is what changes production; `tb build` only touches the development environment.

### Query from your app

```typescript
// src/analytics/query.ts
const READ_TOKEN = process.env.TINYBIRD_READ_TOKEN!; // dashboard_read token

export async function getDailyActiveUsers(days = 30) {
  const res = await fetch(`${TINYBIRD_HOST}/v0/pipes/daily_active_users.json?days=${days}`, {
    headers: { Authorization: `Bearer ${READ_TOKEN}` },
  });
  if (!res.ok) throw new Error(`Tinybird query failed: ${res.status}`);
  return (await res.json()).data as Array<{ date: string; active_users: number }>;
}

export async function getUserActivity(userId: string, days = 7) {
  const url = new URL(`${TINYBIRD_HOST}/v0/pipes/user_activity.json`);
  url.search = new URLSearchParams({ user_id: userId, days: String(days) }).toString();
  const res = await fetch(url, { headers: { Authorization: `Bearer ${READ_TOKEN}` } });
  return (await res.json()).data;
}
```

Change the extension for another format: `.csv`, `.ndjson`, `.parquet`. The JSON answer carries `meta` (column names and types) and `data` (the rows).

For a browser calling Endpoints directly, do not ship the static token. Sign a short-lived JWT (HS256, with `workspace_id`, `name`, `exp` and `scopes`) in your backend and pin the tenant with `fixed_params`, for example `{"type": "PIPES:READ", "resource": "usage_by_customer", "fixed_params": {"customer_id": "cus_4821"}}`.

### Pre-aggregate with a Materialized View

Aggregations that run on every dashboard load should be computed at ingest time.

```text
# tinybird/datasources/daily_events.datasource
SCHEMA >
    `date` Date,
    `event_type` LowCardinality(String),
    `events` AggregateFunction(count)

ENGINE "AggregatingMergeTree"
ENGINE_SORTING_KEY "date, event_type"
```

```text
# tinybird/pipes/daily_events_mv.pipe
NODE rollup
SQL >
    SELECT toDate(timestamp) AS date, event_type, countState() AS events
    FROM events
    GROUP BY date, event_type

TYPE materialized
DATASOURCE daily_events
```

An Endpoint then reads `SELECT date, event_type, countMerge(events) AS events FROM daily_events GROUP BY date, event_type`.

## Examples

### Example 1: Product analytics for a SaaS app

**User prompt:** "Track page views and button clicks from our Next.js app and show daily active users on an admin dashboard."

The agent runs `tb init`, writes `events.datasource` with an `APPEND` token, adds `daily_active_users.pipe` as an Endpoint with a `READ` token, and `tb build` to check it locally. It adds `trackEvents()` to the server code (the ingest token stays in `TINYBIRD_INGEST_TOKEN`), runs `tb --cloud deploy`, and calls `GET /v0/pipes/daily_active_users.json?days=30` from the dashboard backend. The result is `{"data":[{"date":"2026-10-23","active_users":412}, ...]}`.

### Example 2: Usage metering for API billing

**User prompt:** "Count API calls per customer per month so we can bill by usage."

The agent creates an `api_calls` Data Source sorted by `customer_id, timestamp`, a Materialized View that rolls calls up by `customer_id` and month with `countState()`, and an Endpoint `usage_by_customer` with a `customer_id` parameter and `countMerge()`. The billing job reads the Endpoint with a JWT whose `fixed_params` pin one customer, so a customer id cannot be swapped by the caller.

## Guidelines

- **Free plan limits:** 10 GB storage, 0.25 vCPU, 1,000 requests per day per organization, and a hard 429 when the CPU burst is exceeded. It is not "unlimited queries"; check the plan page before promising a dashboard that polls often.
- **Tokens:** use one `APPEND` token for ingestion and one `READ` token per Endpoint group, declared with `TOKEN` in the datafiles. Do not use the workspace admin token in application code, and keep `.tinyb` and `.env.local` out of git.
- **Test locally first:** `tb build` against Tinybird Local or a branch, then `tb deploy --check`. A schema change that is incompatible with the live table fails the deployment unless the Data Source has a `FORWARD_QUERY`.
- **Sorting key and partition key:** choose them for your filters; they cannot be changed later without a migration (`FORWARD_QUERY`).
- **Joins:** ClickHouse joins are fine on small dimension tables; for large tables denormalize at ingest or pre-join in a Materialized View.
- **Duplicates:** the Events API is not idempotent; deduplicate with `ReplacingMergeTree` or at query time if retries matter.
- **Do not use it** as a transactional database: it is built for append-heavy analytics, not row updates.


---
Source attribution: terminal-skills
Original project: https://github.com/TerminalSkills/skills/blob/8a9852d489f274538be9bf1c9121aeb403750b1c/skills/tinybird/SKILL.md
License: Apache-2.0
License information: https://www.apache.org/licenses/LICENSE-2.0
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to the Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by the Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding any notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   Copyright 2025 Terminal Skills

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.



# Mega AI Hub resource metadata

Creator / publisher: terminal-skills
Original project: https://github.com/TerminalSkills/skills/blob/8a9852d489f274538be9bf1c9121aeb403750b1c/skills/tinybird/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Marketing, Sales
Roles: Data Analyst, Growth Marketer, Product Marketer, Marketing Analyst, Business Development
Specialties: API, Database
Tools: Tinybird
Country applicability: Country not specified
Classification basis: Source category, role and specialization labels
Instruction SHA-256: e65664e428e0f2e14e0ffe54b8fa49b56f4963d5cedf2d74d8ed360d7bcbbaa4
