Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit ccf2191

Browse files
committedMar 29, 2024
Document HTTP functions
1 parent ed3b394 commit ccf2191

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: HTTP functions
3+
---
4+
5+
The HTTP functions send HTTP requests to a specified URL.
6+
Using the HTTP functions, you can interact with HTTP servers directly in SQL queries.
7+
They allow to fetch data from external APIs, as well as invoke remote procedure calls.
8+
9+
10+
### Functions
11+
12+
#### http_get()
13+
**``http_get(url: varchar)``** → varchar
14+
15+
**``http_get(url: varchar, headers: array(varchar))``** → varchar
16+
17+
The function sends an HTTP GET request to the given URL, optionally including the provided headers,
18+
and returns the response body as `varchar`.
19+
20+
Often, the response is a JSON document. You can process it further with [JSON-processing SQL functions](json.md).
21+
Other response formats, like html, are also supported.
22+
23+
#### http_post()
24+
**``http_post(url: varchar, body: varchar)``** → varchar
25+
26+
**``http_post(url: varchar, body: varchar, headers: array(varchar))``** → varchar
27+
28+
The function sends an HTTP POST request to the given URL, including the provided data in the request body,
29+
and optionally including the headers. It returns the response body as `varchar`.
30+
31+
The type of the body argument is `varchar`. It gives you flexibility to send JSON data as well as text.
32+
33+
34+
### Access
35+
36+
The HTTP functions are currently in closed Beta. They are available to a group of individual users
37+
and teams approved by Dune. Soon they will be Generally Available free of charge to all Dune paying customers.
38+
39+
40+
### API support and credentials
41+
42+
There are no limits to the supported APIs. The URL can point to any public or private API.
43+
44+
If you need to pass credentials for the API, you can include them in the headers.
45+
For example, you can use the `Authorization` header to pass a token.
46+
If you choose to hardcode the credentials in the query, please make sure to keep the query private.
47+
Optionally, you can use a query parameter as a placeholder:
48+
49+
```sql
50+
SELECT http_get(
51+
'https://api.blastscan.io/api?module=block&action=getblocknobytime&timestamp=1711712564&closest=before&apikey={{api_key}}'
52+
);
53+
```
54+
55+
### Limits
56+
57+
***Call timeout:*** A call issued by an HTTP function times out after 5 seconds.
58+
59+
***Throttling:*** Requests from each query execution are limited to ten requests per second per proxy.
60+
There are currently 3 proxies configured per clusterset. The rate limit is shared between all HTTP function calls
61+
made within the query.
62+
63+
***Response size limit:*** Maximum accepted response body size is 1_000_000 bytes.
64+
65+
***Data size limit:*** Maximum accepted request body size for `http_post()` is 1_000_000 bytes.
66+
67+
68+
### Examples
69+
70+
#### Single HTTP GET request to list coins from CoinGecko
71+
72+
```sql
73+
SELECT http_get('https://api.coingecko.com/api/v3/coins/list');
74+
```
75+
76+
#### Multiple HTTP GET requests parametrized with Dune data
77+
78+
```sql
79+
SELECT
80+
http_get(concat('https://coins.llama.fi/prices/current/ethereum:', CAST(contract_address AS varchar)))
81+
FROM tokens_ethereum.stablecoins;
82+
```
83+
84+
#### JSON processing with http_get()
85+
86+
```sql
87+
SELECT
88+
contract_address,
89+
json_query(
90+
http_get(concat('https://coins.llama.fi/prices/current/ethereum:', CAST(contract_address AS varchar))),
91+
'lax $.coins.*?(@.decimals==18).keyvalue()?(@.name=="symbol" || @.name=="price" || @.name=="decimals").value'
92+
WITH ARRAY WRAPPER
93+
EMPTY ARRAY ON EMPTY
94+
) as coin_data
95+
FROM tokens_ethereum.stablecoins;
96+
```
97+
98+
#### Using http_get() in a subquery as a static filter
99+
By wrapping the `http_get()` function in a subquery, you can use it as a static filter in the main query.
100+
It helps to avoid repeating the same HTTP request multiple times.
101+
102+
```sql
103+
SELECT *
104+
FROM
105+
ethereum.transactions t1,
106+
(SELECT from_hex(json_extract_scalar(http_get('https://api.ensideas.com/ens/resolve/vitalik.eth'),'$.address'))) t2(x)
107+
WHERE t1."from" = t2.x;
108+
```
109+
110+
#### RPC call with http_post()
111+
112+
```sql
113+
SELECT http_post(
114+
'https://docs-demo.quiknode.pro',
115+
'{"method":"eth_chainId","params":[],"id":1,"jsonrpc":"2.0"}',
116+
ARRAY['Content-Type: application/json']);
117+
```
118+
119+
#### Handling quotation marks
120+
If your payload contains the `'` character, you need to quote it properly because it is the bounding character of `varchar`.
121+
Each `'` character should be doubled, and the query engine will unwrap them.
122+
123+
```sql
124+
SELECT json_value(
125+
http_post(
126+
'https://httpbin.org/post',
127+
'foo',
128+
ARRAY['MyHeader : Use two '' quotes']),
129+
'lax $.headers.Myheader');
130+
```

‎docs/query/DuneSQL-reference/Functions-and-operators/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Using ``SHOW FUNCTIONS`` in the query editor returns a list of all available fun
1212
- [Varbinary datatypes](varbinary.md)
1313
- [Base58](base58.md)
1414
- [Chain Utility Functions](chain-utility-functions.md)
15+
- [HTTP Functions](http.md)
1516
- [Varchar Utility Functions](varchar-utility-functions.md)
1617
### Trino Base Functions:
1718
- [Aggregate](aggregate.md)

‎docs/query/DuneSQL-reference/Functions-and-operators/list-by-topic.md

+7
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ For more details, see `geospatial`{.interpreted-text role="doc"}
289289
- `to_geometry`{.interpreted-text role="func"}
290290
- `to_spherical_geography`{.interpreted-text role="func"}
291291

292+
# HTTP
293+
294+
For more details, see `http`{.interpreted-text role="doc"}
295+
296+
- `http_get`{.interpreted-text role="func"}
297+
- `http_post`{.interpreted-text role="func"}
298+
292299
# HyperLogLog
293300

294301
For more details, see `hyperloglog`{.interpreted-text role="doc"}

‎docs/query/DuneSQL-reference/Functions-and-operators/list.md

+2
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ title: List of functions and operators
211211
- `hmac_sha256`{.interpreted-text role="func"}
212212
- `hmac_sha512`{.interpreted-text role="func"}
213213
- `hour`{.interpreted-text role="func"}
214+
- `http_get`{.interpreted-text role="func"}
215+
- `http_post`{.interpreted-text role="func"}
214216
- `human_readable_seconds`{.interpreted-text role="func"}
215217

216218
# I

0 commit comments

Comments
 (0)