Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST API Support #92

Open
scottypate opened this issue Oct 24, 2024 · 0 comments
Open

REST API Support #92

scottypate opened this issue Oct 24, 2024 · 0 comments

Comments

@scottypate
Copy link
Collaborator

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant