How to Query REST APIs with SQL in Preswald

Amrutha GujjarAmrutha Gujjar4 min read

Category: Product


Preswald now supports API-based data sources. That means you can define a REST endpoint in your config file, and Preswald will fetch the response, convert it into a table, and load it into DuckDB behind the scenes — no custom glue code required.

Why This Matters

Many data workflows pull in live information from APIs — whether that’s a weather service, an internal microservice, or a SaaS platform. But traditionally, connecting to APIs means writing brittle fetch/parse logic, serializing JSON, and figuring out how to stitch it together with the rest of your app.

Preswald abstracts all of that. APIs become just another data source — like a CSV or a Postgres table — that you can query with SQL and visualize using Python.

img


How It Works

Let’s say you want to pull data from a public API:

[data.weather]
type = "api"
url = "https://api.weatherapi.com/v1/current.json?q=San+Francisco&key=your_api_key"

Then in your script:

from preswald import get_df, table

df = get_df("weather")
table(df)

Preswald will:

  • Fetch the data from the API

  • Parse the JSON into a flat table (handling nested objects/arrays)

  • Load it into DuckDB

  • Return it as a Pandas DataFrame

From there, you can run SQL queries, join it with other sources, or feed it into charts and dashboards.


Example Use Cases

  • 🛰 Live Dashboards: Track weather, crypto prices, or operational metrics in real time

  • 🔌 Internal Tools: Query internal APIs from microservices alongside databases and CSVs

  • 🔄 Prototyping: Build tools over dynamic data without setting up a pipeline


Combining APIs with Other Data

The real power comes from combining multiple sources in one place. You might join API data with local CSVs, or blend it with metrics from a Postgres table:

from preswald import query

query("""
SELECT t1.temperature_c, t2.population
FROM weather t1
JOIN cities_csv t2 ON t1.location = t2.city
""")

Recap

With this feature, APIs become queryable data tables. You can treat them just like any other source in Preswald — no SDKs, no response parsing, no boilerplate.

Thanks to Lokesh Poluru Velayudham for contributing this powerful addition.

View on GitHubLokesh's Profile