Skip to content

REST API Support #92

@scottypate

Description

@scottypate

Add support for querying a JSON REST API via the httpfs extension.

You can see this in action here, https://motherduck.com/blog/duckdb-110-hidden-gems/.

1. Custom HTTP headers: your database can do API call

The DuckDB extension mechanism is powerful. Most of them are pre-loaded in the background, and you can't see the magic happening. In a previous [blog post](https://motherduck.com/blog/getting-started-gis-duckdb/), I show how we could query an API with a single line statement and return it as a DuckDB table :

CREATE TABLE poi_france AS SELECT * FROM read_json_auto('https://my-endpoint/api')

What is happening here :

    The httpfs extension is loaded to get the data from an HTTP endpoint.
    read_json_auto will parse directly the JSON response in a table

But what if our API is not public and requires authentication and other headers?

This is where the new HTTP headers come into play. You can now create http secret.

CREATE SECRET http (
    TYPE HTTP,
    EXTRA_HTTP_HEADERS MAP {
        'Authorization': 'Bearer sk_test_VePHdqKTYQjKNInc7u56JBrQ'
    }
); 

select unnest(data) as customers 
from read_json('https://api.stripe.com/v1/customers');

Snippet courtesy of [Archie](https://x.com/archieemwood) on [duckdbsnippets.com](https://motherduck.com/blog/duckdb-110-hidden-gems/duckdbsnippet.com).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions