prime-sdk-go

module
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2026 License: Apache-2.0

README ¶

Prime Go SDK README

GoDoc Go Report Card

Overview

The Prime Go SDK is a sample libary that demonstrates the structure of a Coinbase Prime driver for the REST APIs.

Usage

To use the Prime Go SDK, initialize the Credentials struct and create a new client. The Credentials struct is JSON enabled. Ensure that Prime API credentials are stored in a secure manner.

primeCredentials, err := credentials.ReadEnvCredentials("PRIME_CREDENTIALS")
if err != nil {
    log.Fatalf("unable to load prime credentials: %v", err)
}

httpClient, err := client.DefaultHttpClient()
if err != nil {
    log.Fatalf("unable to load default http client: %v", err)
}

client := client.NewRestClient(primeCredentials, httpClient)

The credentials.ReadEnvCredentials is a convenience function to read the credentials from an environment variable and deserialize the JSON structure. Use credentials.UnmarshalCredentials, if pulled from a different source. The JSON format expected by both is:

{
  "accessKey": "",
  "passphrase": "",
  "signingKey": "",
  "portfolioId": "",
  "svcAccountId": "",
  "entityId": ""
}

Coinbase Prime API credentials can be created in the Prime web console under Settings -> APIs. Entity ID can be retrieved by calling Get Portfolio.

Once the client is initialized, instantiate a service to make the desired call. For example, to list portfolios, create the service, pass in the request object, check for an error, and if nil, process the response.

service := portfolios.NewPortfoliosService(client)

response, err := service.ListPortfolios(ctx, &portfolios.ListPortfoliosRequest{})

Error handling

HTTP error responses from the Prime REST API include code, message, subcode, and trace_id. Service methods return these as *errors.ApiError from model/errors. Typed constants are generated from the OpenAPI x-error-codes and x-subcodes lists (hover a constant in an IDE to see its spec description).

import (
    primeerrors "github.com/coinbase/prime-sdk-go/model/errors"
)

resp, err := ordersService.CreateOrder(ctx, req)
if err != nil {
    if apiErr, ok := primeerrors.From(err); ok {
        if primeerrors.IsSubcode(err, primeerrors.SubcodeOrderDuplicateClientOrderId) {
            // handle a duplicate client_order_id
        }
        log.Printf("order failed: %s", apiErr.Format())
    }
    return err
}

A runnable sample is in examples/advanced/errorHandling:

go run ./examples/advanced/errorHandling

Regenerate error constants after fetching a new spec:

make fetch-spec
make gen-errors

Build

To build the sample library, ensure that Go 1.19+ is installed and then run:

go build ./...

🚨 Security and Bug Reports

If you discover a security vulnerability within this SDK, please see our Security Policy for disclosure information.

📧 Contact

License

The Prime Go SDK sample library is free and open source and released under the Apache License, Version 2.0.

Directories ¶

Path Synopsis
examples
createOrder command
createTransfer command
createWallet command
getFcmEquity command
getOrderById command
getOrderPreview command
listActivities command
listOrders command
listPortfolios command
listProducts command
listWallets command
pagination command
errors
Package errors defines Prime REST API error codes, subcodes, and helpers for parsing HTTP error responses returned by this SDK.
Package errors defines Prime REST API error codes, subcodes, and helpers for parsing HTTP error responses returned by this SDK.
tools
gen_errors command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL