Introduction

Tinybird Analytics provides you with an easy way to ingest and query large amounts of data in real time, as well as to automatically create an API to consume those queries. This makes it extremely easy to build fast and scalable applications that query your data; no back-end needed!

Tinybird Analytics works with CSV files. If you have one around (hosted online or in your local drive), you can import it to your account directly through Tinybird’s UI or using our APIs. If you don’t have any .CSV file around, try with a few million Taxi rides from the NYC Taxi Data or with hundreds of thousands of trees from the NYC Tree Atlas.

In order to use Tinybird Analytics APIs, you must use an Auth token with the right permissions depending on whether you want to do. Please, get your tokens from our UI or use a token with TOKEN scope to get those through the API.

As an example, the code below illustrates how you can quickly build a simple frontend application which queries the taxi data mentioned above using Tinybird Analytics, so first, let’s create our first Data Source:

create a datasource with that file
$ curl -F "csv=@green_tripdata_2018-12.csv" \
  -H 'Authorization: Bearer TOKEN' \
  https://api.tinybird.co/v0/datasources?name=rides

Your data has now been imported into a Tinybird Analytics datasource called ‘rides’. Now, in order to prepare and create an API endpoint with it, you will need to create a what we call a Pipe.

A pipe is essentially a sequence of one or more SQL queries (transformation nodes) using one or many Data Sources; you can publish any transformation node of any given Pipe as an API endpoint. This gives you the ability to transform that intial raw CSV data into something that you can publish and use in another app with very little effort.

creating a pipe from an existing Data Source
curl \
-H "Authorization: Bearer TOKEN" \
-d "name=rides_pipe&sql=select * from rides" \
https://api.tinybird.co/v0/pipes

Once the Pipe is created, we can take a look at its content:

get information about the pipe itself
$ curl -H 'Authorization: Bearer TOKEN' \
  https://api.tinybird.co/v0/pipes/rides_pipe
  {
    "id": "t_0c581f4947b046698b30337d2cae511b",
    "name": "rides_pipe",
    "published_version": "t_28460f46fcfc40b4b3d735c0cd588e32",
    "published_date": "2019-07-16 16:07:01.387529",
    "nodes": [
      {
        "id": "t_28460f46fcfc40b4b3d735c0cd588e32",
        "name": "rides_pipe_0",
        "description": null,
        "sql": "select * from rides",
        "dependencies": [
          "rides"
        ],
        "materialized": false,
        "created_at": "2019-07-16 16:07:01.387514",
        "updated_at": "2019-07-16 16:07:01.387514"
      }
    ],
    "created_at": "2019-07-16 16:07:01.387386",
    "updated_at": "2019-07-16 16:07:01.387520"
  }

As you can see, there is only one transformation node in the pipe at first, which by default is a simple query against your Data Source. Let’s now add a transformation to get the number of rides per day.

adds a transformation to get number of rides per day
$ curl \
  -d 'SELECT toDate(lpep_pickup_datetime) date, count() nrides \
  FROM rides GROUP BY date ORDER BY date' \
  -H 'Authorization: Bearer TOKEN' \
  https://api.tinybird.co/v0/pipes/rides_pipe/append
  {
    "id": "t_11fb181f573b47b7a94153778ca2f0fb",
    "name": "rides_pipe_1",
    "description": null,
    "sql": "SELECT toDate(lpep_pickup_datetime) date, count() nrides FROM rides GROUP BY date ORDER BY date",
    "dependencies": [
      "rides"
    ],
    "materialized": false,
    "created_at": "2019-07-16 16:32:06.231977",
    "updated_at": "2019-07-16 16:32:06.231977"
  }

Notice how the query points to rides (the Data Source), but it would also be perfectly fine to do it against the result of the first transformation node rides_pipe_0.

Let’s now publish the node you just created to enable it as an API endpoint. For that, you just have to mark that node as published

publish the node
$ curl \
  -X PUT
  -d 't_11fb181f573b47b7a94153778ca2f0fb' \
  -H 'Authorization: Bearer TOKEN' \
  https://api.tinybird.co/v0/pipes/rides_pipe/published

Once the Pipe is created, and a transformation node has been enabled as an API endpoint, you can get the result of the transformation directly by issuing a GET request to the pipe.

get the data as a regular API
$ curl -H 'Authorization: Bearer TOKEN' \
  https://api.tinybird.co/v0/pipes/rides_pipe.json
  {
    "meta": [
      {
        "name": "date",
        "type": "Date"
      },
      {
        "name": "nrides",
        "type": "UInt64"
      }
    ],
    "data": [
      {
        "date": "2008-12-31",
        "nrides": 1
      },
      {
        "date": "2009-01-01",
        "nrides": 21
      },
      {
        "date": "2010-09-23",
        "nrides": 11
      },
    ...
  }

Or you can use Tinybird.js to query your data from your javascript applications.

Simple tinybird.js application
<!doctype html>
<html lang="en">

  <head>
    <meta charset="utf-8">
    <title>Simple tinybird application</title>
    <script src='https://cdn.tinybird.co/v1/tinybird.js'></script>
  </head>

  <body>
    <script>
      let t = tinybird('<token>').pipe('<pipe>')
      var count = await t.single(`select count() from _`)
      console.log(`your pipe has ${count} elements`)
    </script>
  </body>

</html>

Next steps

Now that you know enough to create your first application with Tinybird Analytics, learn more about how to run queries using Tinybird.js or how to import your data with our REST API.