Python SDK for Tinybird
Maintained. Last release 18 months ago, commits in 5 of the last 26 weeks. Weaker on commit activity and recent releases.
pip install tinybird-python-sdkTinybird Python SDK
SDK around Tinybird APIs.
If you want to manage Workspaces, Data Sources and Pipes you might be looking for the tinybird-cli.
The SDK is meant to programatically ingest NDJSON data or send any request to an API instance.
It contains handlers for:
- logging events to a Tinybird Data Source from your Python module.
- logging events from litellm to a Tinybird Data Source.
Ingest to a Tinybird DataSource
from tb.datasource import Datasource
with Datasource(datasource_name, tinybird_token) as ds:
ds << {'key': 'value', 'key1': 'value1'}
You can also use the async version:
from tb.a.datasource import AsyncDatasource
async with AsyncDatasource(datasource_name, tinybird_token, api_url='https://api.us-east.tinybird.co') as ds:
await ds << {'key': 'value', 'key1': 'value1'}
Notes:
- The
Datasourceobject does some in-memory buffering and uses the events API. - It only supports
ndjsondata - It automatically handles Rate Limits
Ingest using an API instance
from tb.a.api import AsyncAPI
async with AsyncAPI(tinybird_token, api_url) as api:
await api.post('datasources',
params={
'name': 'datasource_name',
'mode': 'append',
'format': 'ndjson',
'url': 'https://storage.googleapis.com/davidm-wadus/events.ndjson',
}
)
- It automatically handles Rate Limits
- Works with any Tinybird API
- The
post,get,sendmethods signatures are equivalent to the requests library.
Logging from your Python module to a Tinybird Data Source
import logging
from tb.logger import TinybirdLoggingHandler
from dotenv import load_dotenv
load_dotenv()
TB_API_URL = os.getenv("TINYBIRD_API_URL")
TB_WRITE_TOKEN = os.getenv("TINYBIRD_WRITE_TOKEN")
logger = logging.getLogger('your-logger-name')
handler = TinybirdLoggingHandler(TB_API_URL, TB_WRITE_TOKEN, 'your-app-name')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)