Python

Complete examples for integrating Turnpike API with Python.

Installation

pip install requests solana python-dotenv websocket-client

Lightning API Examples

Execute Buy Trade

import requests
import os
from dotenv import load_dotenv

load_dotenv()

def execute_buy_trade(public_key: str, mint: str, amount: float, slippage: float = 10):
    """Execute a buy trade using the Lightning API"""

    url = 'https://api.turnpike.dev/trade/buy'
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {os.getenv("TURNPIKE_API_KEY")}'
    }
    payload = {
        'publicKey': public_key,
        'mint': mint,
        'amount': amount,
        'slippage': slippage,
        'priorityFee': 0.0001
    }

    try:
        response = requests.post(url, headers=headers, json=payload)
        response.raise_for_status()

        data = response.json()
        print(f'Trade successful!')
        print(f'Signature: {data["signature"]}')
        print(f'Tokens received: {data["tokensReceived"]}')
        print(f'Effective price: {data["effectivePrice"]}')

        return data
    except requests.exceptions.RequestException as e:
        print(f'Error executing trade: {e}')
        if hasattr(e.response, 'json'):
            print(e.response.json())
        raise

# Usage
execute_buy_trade(
    public_key='YOUR_WALLET_PUBLIC_KEY',
    mint='EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v',
    amount=0.01
)

Get Token Information

Get Portfolio

Local Transaction API Examples

Build and Sign Transaction

WebSocket Examples

Basic WebSocket Connection

Async WebSocket with Websockets Library

Complete Trading Bot Example

Error Handling Example

Async/Await Examples

Last updated