Coinbase Logo

Language and region

Introducing a Command-Line Interface for testing the Coinbase Prime API

TL;DR: This blog post details the Prime command-line interface (CLI), that leverages the Coinbase Prime SDK to provide a simple and effective no-code solution for testing the Coinbase Prime REST API. Developers can use the Prime CLI to quickly test API functionality to help speedup development and integration.

By Jeff Curry

Engineering

, December 13, 2023

Coinbase

Coinbase Prime is at the forefront of comprehensive tooling for institutions and fintechs building a complete cryptocurrency user experience. Tailored for developers and businesses, Coinbase Prime's REST APIs offer a streamlined way to integrate buying, selling, and asset management functionalities into any application, all while maintaining your unique branding and design.

These APIs are engineered to simplify the creation of custom applications, ensuring that the technology aligns with your specific design vision. Whether you are a developer aiming to integrate cryptocurrency transactions into your platform or a business looking to expand your digital asset offerings, Coinbase Prime's APIs provide the flexibility and ease of use necessary for such endeavors.

The introduction of the Prime CLI, powered by the Coinbase Prime SDK, represents a significant leap in testing and development efficiency. This reference application is an essential tool for developers, offering a straightforward way to test every available REST endpoint, thereby enhancing and accelerating the development process.

This blog post explores the capabilities and design of the Prime CLI, offering a comprehensive guide on initiating endpoint tests from your command line. The goal is to streamline your development process, allowing for greater focus on creative application building with less complexity.

Functionality

The Prime CLI is designed to offer a no-code solution for testing the Coinbase Prime API. This approach allows users to perform various operations like placing orders, retrieving transaction details, and executing withdrawals without writing any code. 

For instance, to create a new market order, the following command can be used:

primectl create-order -i ETH-USD -t MARKET -s BUY -b 0.1

Similarly, listing account activities is as straightforward as:

primectl list-activities

Each request within the CLI provides detailed information about the available flags, denoting the required and optional parameters for each endpoint, such as the List Wallets request. By using the -h flag, users can access a comprehensive list of flags:

Screenshot 2023-12-13 at 1.47.46 PM

This level of user-friendliness significantly streamlines the testing process, allowing developers to quickly understand and interact with various endpoints.

High-level Architecture

Regarding architecture, the application maintains a straightforward design. Each script encapsulates the logic necessary for interacting with a specific endpoint. For example, in get_wallet_balance.go, two key functions are defined:

  1. getWalletBalanceCmd: This function creates a command (get-wallet-balance) and constructs a structure with the relevant portfolio and wallet IDs. It then sends a request to the Prime SDK’s GetWalletBalance function and outputs the response.

  2. The second function initializes this command and defines the necessary flags for the endpoint. In the case of wallet balances, 'Wallet ID' is the sole required flag. Notably, 'Portfolio ID' is set as an environment variable during project initialization, eliminating the need to pass it with each request. Users have the option to customize shorthand letters for commands, but this requires rebuilding the project afterwards. 

Example Use Case: Wholesale Clients

Wholesale clients, also known as Introducing Brokers (IBs), play a pivotal role in the onboarding of the next billion users to cryptocurrency. These clients integrate REST APIs into their applications, enabling end-users to buy, sell, and manage digital assets seamlessly. However, grasping the intricacies of cryptocurrency trading and transactions can be a significant challenge, particularly for those new to this domain.

The Prime CLI emerges as a vital tool in this context. It allows these clients to comprehensively test and understand these unique aspects without delving into complex coding. For instance, the create-order-preview command can be used to generate a real-time look at the current price and commission for a given order size and type. This feature is invaluable for clients who wish to gauge market conditions before making actual trades. Similarly, the list-activities command allows clients to track the lifecycle of a withdrawal's approval process, while list-transactions enables clients to observe the status of any transaction. 

Such capabilities of the Prime CLI are essential for wholesale clients to test, evaluate, and visualize the unique properties of cryptocurrency transactions within their applications. This not only ensures a clear understanding of the trading process but also helps in refining the user experience for their end-users.

For developers in the cryptocurrency sector, the Prime CLI offers a hands-on approach to testing and validating API functionalities. For businesses, this translates into a faster rollout of new features and services, granting them an edge in the competitive crypto market. The Prime CLI does more than enhance the development process; it aligns technical development with strategic business objectives. By facilitating a better understanding of market dynamics and user requirements, it enables businesses to tailor their offerings to meet market demands and user expectations effectively.

Running it yourself

From your terminal, navigate to a directory where you wish to store this application. Run the following commands to clone the repository and change directories to it:

git clone https://github.com/coinbase-samples/prime-cli
cd prime-cli

Next, in order to use the Coinbase Prime API, you will need to pass through your Prime credentials. While this logic is handled for you with each request made by the Prime CLI, you will still need to specify your credentials at the start of the application. Coinbase Prime API credentials can be created in the Prime web console under Settings -> APIs. Entity ID can be retrieved by calling Get Portfolio. If you are not configured yet to call this endpoint, you may proceed without including the entityId key and value for the time being, but certain endpoints such as List Invoices and List Assets require it.

PRIME_CREDENTIALS should match the following format, and the following command should be submitted from the terminal window in which you plan to run the Prime CLI:

export PRIME_CREDENTIALS='{
"accessKey":"ACCESSKEY_HERE",
"passphrase":"PASSPHRASE_HERE",
"signingKey":"SIGNINGKEY_HERE",
"portfolioId":"PORTFOLIOID_HERE",
"svcAccountId":"SVCACCOUNTID_HERE",
"entityId":"ENTITYID_HERE"
}'

Next, build the application binary and specify an output name that you will use to call the Prime CLI, e.g., primectl: 

go build -o primectl

To ensure your project's dependencies are up-to-date, run:

go mod tidy

To make your application easily accessible from any location, move the binary you created to a directory that's already in your system's PATH. For example, this is the command to move primectl to /usr/local/bin, as well as set permissions to reduce risk:

sudo mv primectl /usr/local/bin/
chmod 755 /usr/local/bin/primectl

You may now begin to use the Prime CLI. Here are some example commands to help you get started:

primectl list-portfolios

This lists the portfolio(s) associated with the provided API key.

primectl create-order --help

This provides all available flags when creating an order.

primectl create-order-preview -b 0.001 -i ETH-USD -s BUY -t MARKET

This generates an order preview for 0.001 ETH-USD at the current market buy rate. 

- - 

Disclaimer: this application should never be used in any production environment, and any real funds used in this application may be lost. Additionally, this information is provided for informational purposes only and is not investment advice. This is not a recommendation to buy or sell digital assets or to employ a particular investment strategy, codes, or APIs. Coinbase makes no representation on the accuracy, suitability, or validity of any information provided herein.

This material is for informational purposes only and is not (i) an offer, or solicitation of an offer, to invest in, or to buy or sell, any interests or shares, or to participate in any investment or trading strategy, or (ii) intended to provide accounting, legal, or tax advice, or investment recommendations. No representation or warranty is made, expressed or implied, with respect to the accuracy or completeness of the information or to the future performance of any digital asset, financial instrument or other market or economic measure. The information is believed to be current as of the date indicated and may not be updated or otherwise revised to reflect information that subsequently became available or a change in circumstances after the date of publication. Investing in cryptocurrency comes with risk. Prior results do not guarantee future performance. Readers should consult their advisors before making any investment decision. Any references to third parties do not imply Coinbase's endorsement, or approval of any third-party websites or their content. This material is the property of Coinbase, Inc. Any use, review, retransmission, distribution, or reproduction of these materials, in whole or in part, is strictly prohibited in any form without the express written approval of Coinbase. Coinbase, Inc. is licensed to engage in virtual currency business activity by the New York State Department of Financial Services. © 2023 Coinbase, Inc. All Rights Reserved. COINBASE, the C Logo, and the Wallet Logo are all trademarks of Coinbase, Inc.

Coinbase logo
Jeff Curry

About Jeff Curry

Senior Solutions Architect, STP