GPTSCRIPT

Automate DeFi with GPTScript and Purple Flea

GPTScript turns natural language into executable workflows. Write a .gpt file describing what you want — monitor a portfolio, rebalance positions, claim faucet USDC — and it runs. No glue code required.

.gpt Script Format
6 API Products
275 Markets
1% Escrow Fee

Natural Language Scripts that Actually Execute

GPTScript is an open-source scripting framework from Acorn Labs. You write .gpt files in plain English, define tool blocks with HTTP call specs, and GPTScript routes LLM reasoning into structured function calls — no boilerplate agent code needed.

Key insight: In a GPTScript file, your main body is a natural language prompt. The LLM reads it, decides which tools to call, and GPTScript executes the HTTP requests. Purple Flea's REST API is a perfect fit — every product is a clean HTTP endpoint.

📝
Plain English Workflows
Describe your strategy in natural language. GPTScript handles the LLM reasoning and translates intent into typed HTTP calls to Purple Flea endpoints.
SCRIPTING
🔧
Tool Block HTTP Integration
Define Purple Flea API tools once in your .gpt file. GPTScript automatically calls them with the correct parameters based on LLM reasoning.
TOOLS
🔄
Composable Scripts
Import sub-scripts with #!include. Build a library of reusable Purple Flea tools and compose them into complex financial workflows.
COMPOSABLE
🎰
Casino + Faucet
Claim free USDC from the faucet, then play provably fair games. Build casino strategy scripts with GPTScript's loop and conditional constructs.
CASINO
📈
Trading — 275 Markets
Monitor prices, execute market orders, and manage positions across 275 spot and perpetual markets via simple tool definitions.
TRADING
🔐
Trustless Escrow
Automate agent-to-agent payments with GPTScript. Define escrow conditions in natural language — GPTScript determines when to release funds.
ESCROW

Install GPTScript and Run Your First Script

GPTScript is a single binary. Install it, set your API keys, and run a .gpt file. No virtual environments, no dependency management.

1
Install GPTScript
One command installs the GPTScript binary on macOS or Linux. Windows users can use the PowerShell installer.
terminal bash
# Install GPTScript
curl -fsSL https://get.gptscript.ai/install.sh | sh

# Verify installation
gptscript --version

# Set your LLM API key (GPTScript uses OpenAI by default)
export OPENAI_API_KEY="your-openai-key"
# Or use any OpenAI-compatible provider (Ollama, LM Studio, etc.)
export GPTSCRIPT_OPENAI_BASE_URL="http://localhost:1234/v1"

# Set your Purple Flea key
export PURPLE_FLEA_KEY="your-pf-api-key"
2
Understand the .gpt File Format
A .gpt file consists of a main block (natural language goal) and tool blocks (HTTP specs). The main block references tools by name; GPTScript handles the rest.
anatomy.gpt gptscript
---
# This is the MAIN BLOCK (what the agent does)
# It's a natural language description of the goal.
---

Tools: get-balance, get-price, place-order

Check my Purple Flea portfolio balance, get the current BTC price,
and if my USDC balance is above $100 and BTC dropped more than 2%
in the last hour, buy $50 of BTC.

---
name: get-balance
description: Get current wallet balances from Purple Flea
credentials: sys.env.PURPLE_FLEA_KEY as PURPLE_FLEA_KEY

#!http
method: GET
url: https://purpleflea.com/api/wallet/balance
headers:
  Authorization: Bearer ${PURPLE_FLEA_KEY}

---
name: get-price
description: Get current price for a market
args: symbol: trading pair like BTC-USD

#!http
method: GET
url: https://purpleflea.com/api/market/${symbol}/ticker

---
name: place-order
description: Place a market buy or sell order
args: symbol: trading pair, side: buy or sell, usdc_amount: dollar amount
credentials: sys.env.PURPLE_FLEA_KEY as PURPLE_FLEA_KEY

#!http
method: POST
url: https://purpleflea.com/api/trading/market
headers:
  Authorization: Bearer ${PURPLE_FLEA_KEY}
  Content-Type: application/json
body:
  {"symbol": "${symbol}", "side": "${side}", "usdc_amount": ${usdc_amount}}
3
Run Your Script
Execute any .gpt file with a single command. GPTScript streams the reasoning and tool calls to stdout in real time.
terminal bash
# Run a script
gptscript portfolio-rebalance.gpt

# Run with verbose tool call output
gptscript --verbose portfolio-rebalance.gpt

# Pass arguments to the script
gptscript trade.gpt --args '{"target_symbol": "ETH-USD", "budget_usd": 100}'

# Schedule with cron (every hour)
0 * * * * /usr/local/bin/gptscript /home/agent/monitor.gpt >> /var/log/agent.log 2&1

Portfolio Monitor + Auto-Rebalance Script

A complete production script. Monitors portfolio, claims faucet USDC if balance is low, checks market prices, and rebalances to target allocation — all from a single .gpt file.

portfolio-manager.gpt gptscript
---
# Purple Flea Portfolio Manager
# Runs: monitoring, faucet claims, rebalancing, escrow payments
---

Tools: get-portfolio, claim-faucet, get-prices, rebalance-portfolio, casino-bet, escrow-pay
Context: sys.time

You are an autonomous financial agent on Purple Flea (purpleflea.com).

Execute this routine:

1. Get the current portfolio balances.

2. If the total USDC balance is below $5, claim USDC from the faucet.
   The faucet is free for registered agents.

3. Get current prices for BTC-USD, ETH-USD, and SOL-USD.

4. Analyze the portfolio allocation:
   - Target: 40% BTC, 30% ETH, 20% SOL, 10% USDC
   - If any asset is more than 10 percentage points off target, rebalance.
   - Rebalance by selling the overweight asset and buying the underweight one.
   - Use market orders, max $50 per rebalance trade.

5. If BTC dropped more than 5% in 24h, place a $10 crash game bet
   with a 1.5x cashout multiplier as a hedge entertainment trade.

6. Print a summary: current balances, prices, trades executed, reasoning.

---
name: get-portfolio
description: Get all wallet balances and open trading positions
credentials: sys.env.PURPLE_FLEA_KEY as PURPLE_FLEA_KEY

#!http
method: GET
url: https://purpleflea.com/api/portfolio
headers:
  Authorization: Bearer ${PURPLE_FLEA_KEY}

---
name: claim-faucet
description: Claim free USDC from the Purple Flea faucet for new agents
credentials: sys.env.PURPLE_FLEA_KEY as PURPLE_FLEA_KEY

#!http
method: POST
url: https://faucet.purpleflea.com/claim
headers:
  Authorization: Bearer ${PURPLE_FLEA_KEY}
  Content-Type: application/json

---
name: get-prices
description: Get current prices and 24h change for multiple markets
args: symbols: comma-separated trading pairs like BTC-USD,ETH-USD,SOL-USD

#!http
method: GET
url: https://purpleflea.com/api/market/tickers?symbols=${symbols}

---
name: rebalance-portfolio
description: Place a market order to rebalance portfolio
args: symbol: trading pair, side: buy or sell, usdc_amount: dollar amount, reason: reason for rebalance
credentials: sys.env.PURPLE_FLEA_KEY as PURPLE_FLEA_KEY

#!http
method: POST
url: https://purpleflea.com/api/trading/market
headers:
  Authorization: Bearer ${PURPLE_FLEA_KEY}
  Content-Type: application/json
body:
  {"symbol": "${symbol}", "side": "${side}", "usdc_amount": ${usdc_amount}, "reason": "${reason}"}

---
name: casino-bet
description: Place a casino bet on Purple Flea (crash or coinflip)
args: game: crash or coinflip, amount_usdc: bet amount, cashout_multiplier: for crash game
credentials: sys.env.PURPLE_FLEA_KEY as PURPLE_FLEA_KEY

#!http
method: POST
url: https://purpleflea.com/api/casino/bet
headers:
  Authorization: Bearer ${PURPLE_FLEA_KEY}
  Content-Type: application/json
body:
  {"game": "${game}", "amount_usdc": ${amount_usdc}, "cashout_multiplier": ${cashout_multiplier}}

---
name: escrow-pay
description: Send USDC to another agent via Purple Flea trustless escrow
args: recipient_id: agent ID to pay, usdc_amount: amount, memo: payment description
credentials: sys.env.PURPLE_FLEA_KEY as PURPLE_FLEA_KEY

#!http
method: POST
url: https://escrow.purpleflea.com/pay
headers:
  Authorization: Bearer ${PURPLE_FLEA_KEY}
  Content-Type: application/json
body:
  {"recipient_id": "${recipient_id}", "usdc_amount": ${usdc_amount}, "memo": "${memo}"}

Shareable Tool Definitions

Extract your Purple Flea tools into a standalone manifest file so other GPTScript users can import them. Publish to GitHub and anyone can #!include your tools.

purpleflea-tools.gpt gptscript
# Purple Flea Tool Library
# Include in any script:
# #!include github.com/your-user/pf-tools/purpleflea-tools.gpt

name: purple-flea-balance
description: Get Purple Flea wallet balances
credentials: sys.env.PURPLE_FLEA_KEY as PURPLE_FLEA_KEY

#!http
method: GET
url: https://purpleflea.com/api/wallet/balance
headers:
  Authorization: Bearer ${PURPLE_FLEA_KEY}

---
name: purple-flea-trade
description: Execute a market order on Purple Flea
args: symbol: pair, side: buy/sell, usdc_amount: size
credentials: sys.env.PURPLE_FLEA_KEY as PURPLE_FLEA_KEY

#!http
method: POST
url: https://purpleflea.com/api/trading/market
headers:
  Authorization: Bearer ${PURPLE_FLEA_KEY}
  Content-Type: application/json
body:
  {"symbol": "${symbol}", "side": "${side}", "usdc_amount": ${usdc_amount}}
my-strategy.gpt gptscript
# Import shared Purple Flea tools
#!include purpleflea-tools.gpt

Tools: purple-flea-balance, purple-flea-trade

Every morning routine:
1. Check balance
2. If USDC > $200, buy $50 BTC
3. If BTC position > 60% of portfolio, sell some to rebalance
4. Log all actions to output
terminal bash
# Run your strategy
gptscript my-strategy.gpt

# Run on a schedule with systemd
systemctl --user enable pf-agent.timer

# Or with cron
@daily gptscript /opt/agent/my-strategy.gpt