Importing data with Tinybird.js

You can use Tinybird.js to import data from a client side application. There are mainly 3 ways to import your data:

  • Importing a remote file through its URL.

  • Importing a local file

  • Importing or appending data generated and/or gathered by your JavaScript application.

To be able to import data you will need an Auth token with SCOPE:IMPORT permissions. When appending data your Auth token will need SCOPE:APPEND permissions.

importUrl() - import a CSV over HTTP(S)

To create a Datasource from a remote file, use the importUrl() method. The file needs to be accessible via HTTP(S). Importing through a URL is by far the fastest way to import.

importUrl(url, datasource_name[,options])
//norun
// load Demographic Statistics By Zip Code
var res = await tinyb.importUrl('https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.csv?accessType=DOWNLOAD', 'example')
if (res.error) {
  onError(res.error)
} else {
  console.log("imported " + res.datasource.name)
}
Function arguments

NAME

TYPE

DESCRIPTION

url

String

HTTP[s] url where the CSV file is located

datasource_name

String

Optional. Name of the datasource that will be imported. If not specified, Tinybird Analytics will derive a name off of the CSV file name. Can only contain ASCII letters, numbers or underscores. Name must start with a letter

Options[schema]

String

datasource schema in format ‘column1 Type, column2 Type2…’ See clickhouse documentation to learn more about the available column types

Options[append]

Boolean

Default: False. If True, appends data to an existing datasource. Datasource schema must be compatible

Options[headers]

Array(String)

An array of headers to be sent when fetching url

import() - import local files or raw comma-separated values

The import() method creates a datasource and then imports the data provided in the rows argument, which can be one of the following:

An array with multiple rows of comma-separated Strings

import(raw_csv, datasource_name[, options])
//norun
let tinyb = tinybird('<token>')
var rows = [
    ['a', 'b', 'c', 123],
    ['d', 'e', 'f', 456]
]
var res = await tinyb.import(rows, 'my_datasource_name');

A Blob coming, for example, from a form element

import(blob, datasource_name[, options])
//norun
let t = tinybird('<token>')
var fileInputElement = document.getElementById('file_input_form')
var file = fileInputElement.files[0]
var res = await tiny.import(file, 'datasource_name')
Function arguments

NAME

TYPE

DESCRIPTION

rows

Multi

Required.
Array of Strings with comma-separated values
Blob (e.g. coming from a file input element)

datasource_name

String

Optional. Name of the datasource that will be imported. Can only contain ASCII letters, numbers or underscores. Name must start with a letter

Options[schema]

String

Datasource schema in format ‘column1 Type, column2 Type2…’ See clickhouse documentation to learn more about the available column types

Options[headers]

Array(String)

An aray of headers to be sent when fetching url

append() - append data to an existing datasource

Use the append() method to append data to an existing datasource. Target datasource schema should match the rows you are trying to append. The rows argument can be:

A String with raw comma-separated values

append(raw_csv, datasource_name[, options])
//norun
let tinyb = tinybird('<token>').datasource('<table>')
var res = await tiny.append('1,8,Toyota Gazoo Racing,Sébastien BUEMI,Kazuki NAKAJIMA,Fernando ALONSO,,Toyota TS050 - Hybrid,M,LMP1,H,,Classified,388,24:00'52.247,,,5,3'17.658,248.2,,')

An array with multiple rows of comma-separated Strings

append(array[rows], datasource_name[, options])
//norun
let tinyb = tinybird('<token>').datasource('<table>')
var res = await tinyb.append([
    ['a', 'b', 'c', 123],
    ['d', 'e', 'f', 456]
])

A Blob coming, for example, from a form element

append(blob, datasource_name[, options])
//norun
let tinyb = tinybird('<token>').datasource('<table>')
var fileInputElement = document.getElementById('file_input_form')
var file = fileInputElement.files[0]
var res = await tinyb.append(file)

A URL object (importURL could be used in this scenario instead)

append(url, datasource_name[, options])
//norun
let tinyb = tinybird('<token>').datasource('<table>')
var res = await tinyb.append(new URL('http://test.com/file.csv'))
Function arguments

NAME

TYPE

DESCRIPTION

rows

Multi

Required.
String of comma-separated values
Array of Strings with comma-separated values
Blob (e.g. coming from a file input element)
URL object

datasource_name

String

Optional. Name of the datasource that will be imported. If not specified, Tinybird Analytics will derive a name off of the CSV file name. Can only contain ASCII letters, numbers or underscores. Name must start with a letter

Options[headers]

Array(String)

An aray of headers to be sent when fetching url