Create your first Node Red flow using Gemini

Learn how to leverage Google Gemini AI to build automated trading workflows on MachineTrader

Introduction

Node-RED is even easier to use with the latest advances in AI models. In this example, we will use the free version of Gemini to create a Node-RED Flow. Note that the prompt refers to the Alpaca API keys that were stored in your instance when you activated your Alpaca paper account.

Enter the Following Prompt into Gemini

Navigate to gemini.google.com and enter the following prompt:

"Write a flow in NodeRed that gets the balance of my Alpaca account. Use the api keys: global.get('apiKeyPaper') global.get('apiSecretPaper')"

Gemini's Response

Gemini will return the following:

To get your Alpaca Paper account balance in Node-RED using your specific global variables, you can use a Function node to set the authentication headers followed by an HTTP Request node.

1. The Logic Flow

You'll need a flow consisting of three main nodes:

  • Inject node: To trigger the request (manually or on a timer).
  • Function node: To grab your global keys and format the request headers.
  • HTTP Request node: To call the Alpaca API.

2. Node Configuration

A. The Function Node

Paste the following code into a Function node. This code retrieves your keys and sets the required headers for Alpaca's API:

// Retrieve keys from global storage
const apiKey = global.get('apiKeyPaper');
const apiSecret = global.get('apiSecretPaper');

// Set the URL for the Paper Trading account endpoint
msg.url = "https://paper-api.alpaca.markets/v2/account";
msg.method = "GET";

// Configure Alpaca Auth Headers
msg.headers = {
    "APCA-API-KEY-ID": apiKey,
    "APCA-API-SECRET-KEY": apiSecret,
    "Accept": "application/json"
};

return msg;

B. The HTTP Request Node

Set up the HTTP Request node with these settings:

  • Method: - set by msg.method - (or just GET)
  • URL: Leave blank (it's handled by the Function node)
  • Return: a parsed JSON object

3. The JSON Flow (Copy & Import)

The easiest way to import a Node-RED flow is to copy the JSON and import it directly. You can import this directly into Node-RED by going to Menu > Import and pasting this JSON:

[{"id":"1","type":"inject","z":"f6","name":"Trigger","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":100,"wires":[["2"]]},{"id":"2","type":"function","z":"f6","name":"Set Headers","func":"const apiKey = global.get('apiKeyPaper');\nconst apiSecret = global.get('apiSecretPaper');\n\nmsg.url = \"https://paper-api.alpaca.markets/v2/account\";\nmsg.method = \"GET\";\n\nmsg.headers = {\n    \"APCA-API-KEY-ID\": apiKey,\n    \"APCA-API-SECRET-KEY\": apiSecret,\n    \"Accept\": \"application/json\"\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":100,"wires":[["3"]]},{"id":"3","type":"http request","z":"f6","name":"Alpaca API","method":"use","ret":"obj","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"headers":[],"x":490,"y":100,"wires":[["4"]]},{"id":"4","type":"debug","z":"f6","name":"Balance","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.equity","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":100,"wires":[]}]

How to Import the Flow

  1. Copy the JSON code above
  2. Go to your MachineTrader instance and navigate to "Build Your First Flow"
  3. Click the menu icon (☰) in the top-right corner of the Node-RED editor
  4. Select "Import" from the dropdown menu
  5. Paste the JSON configuration
  6. Click "Import" to add the flow to your workspace
Node-RED Menu showing Import option

Gemini produced a functioning Node Red flow just exactly as we requested completed with a starting inject node name Trigger, a function node called "Set Headers" that retrieves the stored API keys, an http request node "Alpaca API" that passess the request to Alpaca's API, and finally a debug node called "Balance" that displays the account balance in the debut panel on the right side panel.

However, we might want to see all of the account information; not just the balance. To do that, we can copy the debug node, made a simple edit requesting ".equity" from the msg.payload so we get all of the account information and rename the node "account info."

Step 2

Step 3

Step 4

Key Values to Look For

Once you trigger the flow, the Debug node will output your account details. The most common "balance" metrics in the response are:

  • payload.equity: Your total account value (Cash + Unrealized P&L).
  • payload.cash: The actual settled cash available.
  • payload.buying_power: Your current limit for opening new positions.

Note: Ensure your global variables are actually set before running this, otherwise the headers will be undefined and Alpaca will return a 401 Unauthorized error.