Skip to content

Python Examples

Setup

bash
pip install requests
python
import requests
import os

TOKEN = os.environ['CELDRAX_TOKEN']  # cxk_...
DB    = 'mydb'
BASE  = 'https://celdrax.com'

def query(table, body):
    res = requests.post(
        f'{BASE}/api/{DB}/{table}',
        json=body,
        headers={'Authorization': f'Bearer {TOKEN}'},
    )
    res.raise_for_status()
    return res.json()['data']

SELECT

python
# Get all active users
data = query('users', {
    'verb': 'select',
    'filters': {'active': True},
    'orderBy': 'created_at DESC',
    'limit': 50,
})

for row in data['rows']:
    print(row['email'])

INSERT

python
data = query('users', {
    'verb': 'insert',
    'data': {
        'email': 'alice@example.com',
        'name': 'Alice',
        'active': True,
    },
})

print(f"Created user with id: {data['id']}")

UPDATE

python
data = query('users', {
    'verb': 'update',
    'data':    {'active': False},
    'filters': {'id': 42},
})

print(f"Updated {data['updated']} row(s)")

DELETE

python
data = query('sessions', {
    'verb': 'delete',
    'filters': {'user_id': 42},
})

print(f"Deleted {data['deleted']} row(s)")

Error handling

python
from requests.exceptions import HTTPError

try:
    data = query('users', {'verb': 'select'})
except HTTPError as e:
    err = e.response.json()
    if err.get('code') == 'REQUESTS_EXCEEDED':
        print('Monthly limit reached — upgrade your plan')
    else:
        print(f"Error: {err['error']}")