Skip to content

Hello World

The smallest possible loop: create a table, insert data, query it, drop it.

What you'll build

A table with three rows. A SELECT query. A cleanup. Nothing else.

Code

import requests

API_URL = "https://api.deeplake.ai"
TOKEN = "YOUR_TOKEN"
WORKSPACE = "YOUR_WORKSPACE"
TABLE = "hello"

headers = {
    "Authorization": f"Bearer {TOKEN}",
    "Content-Type": "application/json",
}

def query(sql):
    res = requests.post(
        f"{API_URL}/workspaces/{WORKSPACE}/tables/query",
        headers=headers,
        json={"query": sql},
    )
    return res.json()

# Create — schema-qualify with workspace and use USING deeplake
query(f"""
    CREATE TABLE IF NOT EXISTS "{WORKSPACE}"."{TABLE}" (
        id SERIAL PRIMARY KEY,
        message TEXT
    ) USING deeplake
""")

# Insert
for msg in ["Hello, Deep Lake!", "Vector search is neat.", "SQL is the interface."]:
    query(f'INSERT INTO "{WORKSPACE}"."{TABLE}" (message) VALUES (\'{msg}\')')

# Note: Deep Lake tables have eventual consistency — data may take
# a few seconds to appear in SELECT after INSERT.

# Query
result = query(f'SELECT * FROM "{WORKSPACE}"."{TABLE}" ORDER BY id')
for row in result.get("rows", []):
    print(row)

# Clean up
query(f'DROP TABLE IF EXISTS "{WORKSPACE}"."{TABLE}"')

What to try next