Quickstart: Make your first API call
These steps assume you already have a Coinbase Developer Platform account. Please sign up here if not.
This Coinbase Staking Quickstart will walk you through installing our API client and making your first API call to our ListProtocols
endpoint. Our native clients provide access to all API features and are written for easy integration to your backend.
1. Set up a workspace
In your preferred command shell, let's get started by creating a new workspace directory staking-demo
and navigating to it.
mkdir staking-demo
cd staking-demo
2. Install the pre-requisites
Install the following prerequisites and dependency packages for your chosen language.
- Typescript
- Golang
The Ts staking client requires installation of Node (version 20+) before proceeding ahead.
Once you have node
and npm
installed, you can install the following packages using the command below.
npm install ts-node typescript
The Go staking client requires installation of Go (version 1.21+) before proceeding ahead.
3. Setup API access
Ensure you keep your API key file secure and do not share it with anyone. If you suspect your key has been compromised, you can regenerate it in the CDP portal and/or mark the compromised key as Disabled
.
-
Login to the Coinbase Developer Platform (CDP) and create an API key.
-
Create a new API key in the portal and paste the
apiKeyName
andapiPrivateKey
info into the relevant example code below.
4. Install the Staking Client
Install the package and import it in your code to access Coinbase Staking features using a native language you're most familiar with.
- Typescript
- Golang
5. Make an API call
In the example below, you can learn how to create a StakingClient with
your saved credentials and call ListProtocols
to retrieve all the protocols
supported by Coinbase Staking.
- Typescript
- Golang
Create a new typescript file with the name example.ts
and place it within your workspace directory staking-demo
. Copy and paste the code example below into this file.
import { StakingClient } from "@coinbase/staking-client-library-ts";
const apiKeyName: string = 'your-api-key-name';
const apiPrivateKey: string = 'your-api-private-key';
const client = new StakingClient();
client.listProtocols().then((response) => {
console.log(response);
});
Run the example using ts-node
. The response will be shown in your output window.
ts-node example.ts
Create a new Go file with the name example.go
and place it within your workspace directory staking-demo
. Copy and paste the code example below into ths file.
package main
import (
"context"
"fmt"
"log"
"google.golang.org/protobuf/encoding/protojson"
"github.com/coinbase/staking-client-library-go/auth"
"github.com/coinbase/staking-client-library-go/client"
"github.com/coinbase/staking-client-library-go/client/options"
api "github.com/coinbase/staking-client-library-go/gen/go/coinbase/staking/orchestration/v1"
)
var (
apiKeyName = "your-api-key-name"
apiPrivateKey = "your-api-private-key"
)
func main() {
ctx := context.Background()
apiKey, err := auth.NewAPIKey(auth.WithLoadAPIKeyFromFile(true))
if err != nil {
log.Fatalf("error loading API key: %s", err.Error())
}
// Create a staking client.
stakingClient, err := client.New(ctx, options.WithAPIKey(apiKey))
if err != nil {
log.Fatalf("error instantiating staking client: %s", err.Error())
}
// List all protocols.
protocols, err := stakingClient.Orchestration.ListProtocols(ctx, &api.ListProtocolsRequest{})
if err != nil {
log.Fatalf("error listing protocols: %s", err.Error())
}
marshaled, err := protojson.MarshalOptions{Indent: " ", Multiline: true}.Marshal(protocols)
if err != nil {
log.Fatalf("error marshaling reward: %s", err.Error())
}
fmt.Println(string(marshaled))
}
Let's quickly now download the missing dependencies in our example.go
file and then run it. The response will be shown in your output window.
go mod tidy
go run example.go
Expected output
{
"protocols": [
{
"name": "protocols/ethereum_kiln"
},
{
"name": "protocols/solana"
}
]
}
Next Steps
For protocol specific examples and explanation of key functionality, check out their respective guides: