Skip to main content

Python SDK Quickstart

Get up and running with the TABStack Python SDK in minutes. This guide will walk you through installation, authentication, and your first API call.

Prerequisites

  • Python >= 3.10
  • Package manager: pip, uv, poetry, or pipenv
  • TABStack API key from console.tabstack.ai

Installation

Install the SDK using your preferred package manager:

uv pip install tabstack

Or add to your project:

uv add tabstack

Get Your API Key

Before you can start using the SDK, you'll need to create an API key:

  1. Visit the TABStack Console
  2. Sign in to your account (or create one if you haven't already)
  3. Navigate to the API Keys section and click "Manage API Keys"
  4. Click "Create New API Key"
  5. Give your key a descriptive name (e.g., "Development", "Production")
  6. Copy the generated API key and store it securely

⚠️ Important: Your API key will only be shown once. Make sure to copy and store it in a secure location.

Set Up Environment Variable

For security and convenience, store your API key as an environment variable:

macOS/Linux:

# Add to your shell profile (~/.bashrc, ~/.zshrc, or ~/.bash_profile)
export TABSTACK_API_KEY="your_api_key_here"

# Or set it temporarily for the current session
export TABSTACK_API_KEY="your_api_key_here"

# Reload your shell or run:
source ~/.bashrc # or ~/.zshrc

Windows (Command Prompt):

# Set temporarily for current session
set TABSTACK_API_KEY=your_api_key_here

# Set permanently (requires restart)
setx TABSTACK_API_KEY "your_api_key_here"

Windows (PowerShell):

# Set temporarily for current session
$env:TABSTACK_API_KEY = "your_api_key_here"

# Set permanently for current user
[Environment]::SetEnvironmentVariable("TABSTACK_API_KEY", "your_api_key_here", "User")

Your First API Call

The Python SDK is fully async and uses the async context manager pattern for automatic resource cleanup.

import asyncio
import os
from tabstack import TABStack

async def main():
# Initialize the client with async context manager
async with TABStack(api_key=os.getenv('TABSTACK_API_KEY')) as tabs:
try:
# Extract markdown from a URL
result = await tabs.extract.markdown('https://example.com')
print(result.content)
except Exception as error:
print(f'Error: {error}')

# Run the async function
asyncio.run(main())

Sync Example

If you prefer synchronous code, use TABStackSync:

import os
from tabstack import TABStackSync

# Initialize the sync client with context manager
with TABStackSync(api_key=os.getenv('TABSTACK_API_KEY')) as tabs:
try:
# Extract markdown from a URL (no async/await needed)
result = tabs.extract.markdown('https://example.com')
print(result.content)
except Exception as error:
print(f'Error: {error}')

Response:

---
title: Example Domain
description: Example Domain
url: https://example.com
type: website
---

# Example Domain

This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.

[More information...](https://www.iana.org/domains/example)

Core Features

The SDK provides three main operators for working with web content:

Extract

Convert web pages to structured data:

  • Markdown: Convert HTML to clean Markdown format
  • Schema: Auto-generate JSON schemas from web content
  • JSON: Extract structured data matching your schema

Learn more about Extract features →

Generate

Transform web content using AI:

  • Summarize and analyze content
  • Categorize and tag data
  • Perform sentiment analysis
  • Extract key insights with custom instructions

Learn more about Generate features →

Automate

Execute browser automation tasks with natural language:

  • Web scraping with real-time updates
  • Form filling and submission
  • Multi-step workflows
  • Streaming progress events

Learn more about Automate features →

Quick Example: Extract Structured Data

Here's a more advanced example that extracts structured data from a web page:

import asyncio
import os
from tabstack import TABStack

async def main():
async with TABStack(api_key=os.getenv('TABSTACK_API_KEY')) as tabs:
# Define the data structure you want
schema = {
"type": "object",
"properties": {
"stories": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {"type": "string"},
"points": {"type": "number"},
"author": {"type": "string"}
},
"required": ["title", "points", "author"]
}
}
},
"required": ["stories"]
}

try:
# Extract data matching the schema
result = await tabs.extract.json(
url='https://news.ycombinator.com',
schema=schema
)

print(f"Found {len(result.data['stories'])} stories:")
for story in result.data['stories'][:3]: # Show first 3
print(f"- {story['title']} ({story['points']} points) by {story['author']}")

except Exception as error:
print(f'Error: {error}')

asyncio.run(main())

Connection Pooling

The Python SDK includes built-in connection pooling for optimal performance when making multiple requests:

import asyncio
import os
from tabstack import TABStack

async def main():
# Configure connection pooling
async with TABStack(
api_key=os.getenv('TABSTACK_API_KEY'),
max_connections=100, # Max concurrent connections
max_keepalive_connections=20, # Max idle connections to keep alive
keepalive_expiry=30.0, # Seconds to keep connections alive
timeout=60.0 # Request timeout in seconds
) as tabs:
# Make multiple concurrent requests efficiently
tasks = [
tabs.extract.markdown(f'https://example.com/page{i}')
for i in range(10)
]
results = await asyncio.gather(*tasks, return_exceptions=True)

for i, result in enumerate(results):
if isinstance(result, Exception):
print(f"Page {i} failed: {result}")
else:
print(f"Page {i}: {len(result.content)} characters")

asyncio.run(main())

Type Hints

The SDK is fully typed with comprehensive type hints for better IDE support:

import asyncio
import os
from tabstack import TABStack, MarkdownResponse, JsonResponse

async def main():
async with TABStack(api_key=os.getenv('TABSTACK_API_KEY')) as tabs:
# IDE knows result is MarkdownResponse
result: MarkdownResponse = await tabs.extract.markdown('https://example.com')

# Access typed fields
content: str = result.content
url: str = result.url

# JSON response is also typed
schema = {"type": "object", "properties": {"title": {"type": "string"}}}
json_result: JsonResponse = await tabs.extract.json('https://example.com', schema)

# Access data as dict
data: dict = json_result.data

asyncio.run(main())

Async Best Practices

Using Async Context Manager

Always use the async context manager to ensure proper cleanup:

# ✅ Good: Automatic cleanup
async with TABStack(api_key=api_key) as tabs:
result = await tabs.extract.markdown(url)

# ❌ Not recommended: Manual cleanup required
tabs = TABStack(api_key=api_key)
try:
result = await tabs.extract.markdown(url)
finally:
await tabs.close()

Concurrent Requests

Use asyncio.gather() for concurrent requests:

import asyncio

async def fetch_multiple():
async with TABStack(api_key=api_key) as tabs:
# Run multiple requests concurrently
results = await asyncio.gather(
tabs.extract.markdown('https://example.com/1'),
tabs.extract.markdown('https://example.com/2'),
tabs.extract.markdown('https://example.com/3'),
return_exceptions=True # Don't fail all if one fails
)

for i, result in enumerate(results):
if isinstance(result, Exception):
print(f"Request {i} failed: {result}")
else:
print(f"Request {i} succeeded")

Error Handling

Always wrap API calls in try-except blocks:

async def safe_extract():
async with TABStack(api_key=api_key) as tabs:
try:
result = await tabs.extract.markdown('https://example.com')
return result
except Exception as error:
print(f"Extraction failed: {error}")
return None

Next Steps

Now that you're up and running:

  1. Extract Features: Learn how to extract markdown, generate schemas, and pull structured JSON data
  2. Generate Features: Discover AI-powered content transformation and analysis
  3. Automate Features: Execute complex browser automation tasks with streaming updates
  4. Error Handling: Build robust applications with proper error handling

Need Help?