This page describes how to query the Blockchain RPC service using an API key for authentication.
Enable the Blockchain RPC API
To use the service you must first enable the Blockchain RPC API for your project.
Navigate to the Blockchain RPC API:
Click Enable.
Create an API key
You must use an API key for authentication to query the service.
To generate a key in the Google Cloud console:
In the Google Cloud console, go to the APIs & Services, Credentials page.
Click Create credentials.
Click API key.
In the API key created dialog, click Using the RPC endpoint.
Copy to copy the key, and then paste it into a temporary file. You will need the API key in
See Authenticate using API keys for more information on API keys.
Using the RPC endpoint
Now that you've created an API key, you can begin making requests. A full
list of all the RPC methods available are listed in the RPC API
reference documentation. Our example request calls the eth_blockNumber
method.
curl -X POST -H "Content-Type: application/json" -d '{
"jsonrpc": "2.0",
"id": 1,
"method":
"eth_blockNumber",
"params": []}' \
https://blockchain.googleapis.com/v1alpha1\
/projects/PROJECT_ID\
/locations/LOCATION\
/endpoints/NETWORK\
/rpc?key=API_KEY
Where:
- PROJECT_ID is your Google Cloud project ID.
For example,
my-project-id
. This may be different than your project name. You can find the project ID, under Project info on the dashboard page. - LOCATION is the region of your endpoint. At the moment, only
us-central1
is supported but other regions will be launching soon. - NETWORK is the name of the blockchain network that
you want to query. Valid values are
ethereum-mainnet
,ethereum-holesky
. - API_KEY is the API key you created in the prior step.
After executing this curl request, you should see a response like:
{"jsonrpc":"2.0","id":1,"result":"0x13acb8d"}
Using the RPC endpoint with WebSocket subscriptions
WebSocket support is also enabled for Blockchain RPC endpoints. This example uses the wscat open source project.
wscat -c wss://blockchain.googleapis.com/v1alpha1\
/projects/PROJECT_ID\
/locations/LOCATION\
/endpoints/NETWORK\
/rpc?key=API_KEY
Subscribe to new block headers:
{"id":1,"jsonrpc":"2.0","method":"eth_subscribe","params":["newHeads"]}
You'll start receiving messages for each new block header.