description | sidebar_position |
---|---|
Learning about curl |
5 |
The Client Uniform Resource Locator (curl) command line tool sends API requests via URLs and returns responses.
Requests to Infura APIs commonly use curl.
:::info
We recommend the resource Everything curl.
:::
Your operating system may include curl, or you may need to download and install it.
Many Infura requests take the form:
curl https://mainnet.infura.io/v3/<YOUR-API-KEY> \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": 1}'
The code snippet above is an API call to Infura's Ethereum mainnet endpoint.
It requests the latest block number using the method eth_blockNumber
.
Let's step through each line of the code snippet to understand what's happening.
The first line uses the curl
command to send a request to the URL https://mainnet.infura.io/v3/<YOUR-API-KEY>
.
curl https://mainnet.infura.io/v3/<YOUR-API-KEY> \
:::info
Replace <YOUR-API-KEY>
with a specific API key.
:::
The -X
flag specifies a common HTTP method.
-X POST \
The -H
or --header
flag specifies header information.
The example specifies the Content-Type
to be application/json
which means the requested resource is a JavaScript Object Notation (JSON) object.
-H "Content-Type: application/json" \
The -d
or --data
flag specifies more information sent along with the curl request.
In the example, the data object is a list of key value pairs in JSON format. This follows the JSON RPC 2.0 specification which requires the four specific keys seen here.
The method
, params
, and id
values are modifiable.
The jsonrpc
value is required by the specification.
-d '{"jsonrpc": "2.0", "method": "eth_blockNumber", "params": [], "id": 1}'
Enter curl code in a terminal window, or a command line tool from your computer's operating system and click return.
curl returns a response object if the request is successful.
{"jsonrpc": "2.0", "id": 3, "result": "0xe0a763"}