Quickstart: Blockchain RPC

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

Using the RPC endpoint

  1. In the Google Cloud console, go to the Blockchain RPC page.

    Go Blockchain RPC

  2. From the console, copy the JSON-RPC endpoint that matches the network and location combination you need.

    Blockchain RPC quickstart

  3. Now 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": []}' \
JSON_RPC_ENDPOINT

Where:

  • JSON_RPC_ENDPOINT is an endpoint you copied from the console. This endpoint includes a default API key that has been automatically created. You can manage all of your keys on the Credentials page.

After executing this curl request, you should see a response like:

{"jsonrpc":"2.0","id":1,"result":"0x13acb8d"}

Using WebSocket subscriptions

WebSocket support is also enabled for Blockchain RPC endpoints. This example uses the wscat open source project.

  1. In the Google Cloud console, go to the Blockchain RPC page.

    Go Blockchain RPC

  2. From the console, copy the WebSocket endpoint that matches the network and location combination you need.

    Blockchain RPC quickstart

  3. Now you can begin making requests.

wscat -c WS_ENDPOINT

Where:

  • WS_ENDPOINT is a WebSocket endpoint from the console.

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.