Search code examples
coinbase-api

CoinBase Pro API - Typescript API


I am new to TypeScript, but I have been working on this for a few weeks, trying to set-up a quick trading bot on CoinBase PRO. I am using the tutorial script here:

https://coinbase.github.io/coinbase-pro-trading-toolkit/cbptt_tutorials_triggers.html

The page here shows this code:

<code>const options: CoinbaseProFeedConfig = {
    logger: logger,
    auth: { key: null, secret: null, passphrase: null}, // use public feed
    channels: ['ticker'],
    wsUrl: COINBASE_PRO_WS_FEED,
    apiUrl: COINBASE_PRO_API_URL
};

CBPTT.Factories.CoinbasePro.getSubscribedFeeds(options, [product]).then((feed: CoinbaseProFeed) => {
   ...
});</code>

And provides this text: "Note that we nulled out the auth object to force the feed to use unauthenticated messages. You can set auth: null to just use the defaults, which since you have your Coinbase Pro API keys set in the environment, will automatically use those and receive authenticated messages (nice if you want to confirm when your trades are filled)."

However, in looking through the other tutorial and Coinbase Pro (CBP) API documentation, although there are several references to adding your CBP API keys to the "environmental variables" or "envars" there is no description or tutorial that shows HOW to do this.

Is this something so basic in TypeScript that everyone knows how to do this?

In another area of the tutorials, I was able to figure out that I could add values to to the AUTH variable like this:

<code>
const options: CoinbaseProFeedConfig = {
    logger: logger,
    apiUrl: process.env.COINBASE_PRO_API_URL || 'https://api.pro.coinbase.com',
    auth: {
        key: process.env.COINBASE_PRO_KEY || 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        secret: process.env.COINBASE_PRO_SECRET || 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx+xxXX==',
        passphrase: process.env.COINBASE_PRO_PASSPHRASE || 'xxxxxxxxxx'
    },
    channels: ['ticker'],
    wsUrl: COINBASE_PRO_WS_FEED
};
</code>

Doing this directly as above where the XXXX's represent my actual keys seems to work fine for "CoinbaseProFeedConfig" . . . which I have successfully used to retrieve my account balances.

However, when I try to do something similar in the "SubmitTrade" function on the original tutorial, the AUTH variable cannot be added to the "coinbaseProAPI" script int he following code:

<code>
const coinbaseProAPI = CBPTT.Factories.CoinbasePro.DefaultAPI(logger);
function submitTrade(side: string, amount: string) {
    const order: PlaceOrderMessage = {
        type: 'order',
        time: null,
        productId: product,
        orderType: 'market',
        side: side,
        size: amount
    };
    coinbaseProAPI.placeOrder(order).then((result: LiveOrder) => {
        pushMessage('Order executed', `Order to sell 0.1 ${base} placed. Result: ${result.status}`);
    });
}

</code>

Has anyone used Typescript successfully to TRADE crypto on Coinbase Pro? What am I missing? How do I set the "envars" or "environmental variables" properly to allow this?

The full code for the tutorial I am trying to modify can be seen here:

https://github.com/coinbase/coinbase-pro-trading-toolkit/blob/master/tutorials/t005_alertTrader.ts

After several weeks of tearing my hair out . . . I'm still stumped.

Thanks in advance for your assistance!


Solution

  • So after another week or so of scratching my head, I think I can answer my own question. Sometime it just comes down to what you type into google. ;)

    At any rate, after doing more digging on "process.env" . . . search results return this URL, which discusses how to develop a very basic web menu in TypeScript . . . this tutorial also discusses environmental variables and how to access them:

    https://auth0.com/blog/use-typescript-to-create-a-secure-api-with-nodejs-and-express-getting-started/

    Thanks to Dan Arias for his excellent original article. I am going to copy some pertinent parts below (with some added noob commentary), for anyone who might be searching for guidance on environmental variables in TypeScript:

    Install Project Dependencies

    Your Node.js project requires a couple of dependencies to create a secure Express server with TypeScript. Install them like so:

    In your IDE terminal type:

    npm i express dotenv
    

    Here's what the above package does in your project:

    dotenv: Zero-dependency module that loads environment variables from a .env file into process.env.

    Use Environmental Variables

    Instead of using hard-coded configuration variables within files throughout your project, you can define all those variables in a central location and load them into the file modules that need them. This central location is commonly defined as a hidden file named .env, which you can create as follows (Type this into your IDE terminal):

    touch .env
    

    As stated above, this creates a hidden file in your project directory called .env

    I am developing in MS Windows (With Eclipse IDE), so I used file explorer to navigate to the hidden file, since I couldn't see it in my IDE to edit it. (Obviously in the Windows OS you'll need to show hidden files in order to see it.)

    I edited the .env file in NotePad++ and added the environmental variables I needed in my project. (In this case for the Coinbase Pro Trading Toolkit) The contents of the .env file look something like this:

    # COINBASE API STUFF
    COINBASE_PRO_KEY='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    COINBASE_PRO_SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxXXXX'
    COINBASE_PRO_PASSPHRASE='xxxxxxxxxxx'
    

    Where the XXX's represent my actual API values (of course.)

    As seen in the next section, any module of your project can load the variables defined within .env using the dotenv package.

    At the top of your TypeScript application include the external dependencies:

    /*
     * Required External Modules
     */
    
    import * as dotenv from "dotenv";
    
    dotenv.config();
    

    After this, you can begin using the environmental variables in your application by addressing them as follows (This simply prints the value of the variables to the console.log using a couple of different methods:

    const key:any = process.env.COINBASE_PRO_KEY;
    
    console.log(`KEY envar is ${key}`);
    console.log(`PASSPHRASE envar is ${process.env.COINBASE_PRO_PASSPHRASE}`);
    

    I hope this is enough to get you started with your project.

    If you have questions please add them below. Good luck!