This script retrieves current stock prices from the Twelve Data API and updates the corresponding rows in a SeaTable table. It demonstrates how to call external APIs from a SeaTable Python script.

You need a free API key from Twelve Data and a table with columns Symbol (text) and Price (number).

from seatable_api import Base, context
import requests

base = Base(context.api_token, context.server_url)
base.auth()

TABLE_NAME = "Stocks"
API_KEY = "your-api-key"

if API_KEY == "your-api-key":
    print("ERROR: Please set your Twelve Data API key first.")
    print("Get a free key at https://twelvedata.com/")
else:
    rows = base.list_rows(TABLE_NAME)
    for row in rows:
        symbol = row.get('Symbol')
        if not symbol:
            continue

        url = f"https://api.twelvedata.com/price?symbol={symbol}&apikey={API_KEY}"
        response = requests.get(url)
        data = response.json()

        if 'price' in data:
            base.update_row(TABLE_NAME, row['_id'], {
                'Price': float(data['price'])
            })
            print(f"{symbol}: {data['price']}")
        else:
            print(f"API error: {data.get('message', 'unknown error')}")
            break

    print("---")
    print("Stock prices updated.")

You can replace the Twelve Data API with any other financial data provider. Adjust the URL and response parsing accordingly.

For the complete function reference, visit the SeaTable Developer Manual .