Quickstart: Blockchain RPC

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.

  1. Navigate to the Blockchain RPC API:

    Go to Blockchain API

  2. 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:

  1. In the Google Cloud console, go to the APIs & Services, Credentials page.

    Go to Credentials

  2. Click Create credentials.

    Blockchain RPC quickstart

  3. Click API key.

  4. In the API key created dialog, click Copy to copy the key, and then paste it into a temporary file. You will need the API key in Using the RPC endpoint.

See Authenticate using API keys for more information on API keys.

Configure an RPC endpoint

To use Blockchain RPC, you must configure an endpoint for your project and API key:

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.

Using the RPC endpoint

Now that you've configured your endpoint, 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

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.