Skip to content

Latest commit

 

History

History

README.md

Bybit Go SDK - Examples

This directory contains working examples demonstrating how to use the Bybit Go SDK.

Prerequisites

  1. Set up environment variables for authenticated examples (public market-data and kline receipt examples require no API keys):
export BYBIT_API_KEY="your_api_key"
export BYBIT_API_SECRET="your_api_secret"
  1. Install dependencies:
cd ..
go mod tidy

Running Examples

Each example is a standalone Go program. Run them individually:

Basic Client Example

Demonstrates basic client initialization and server time retrieval.

go run basic_client/main.go

Market Data Example

Shows how to fetch market data for multiple symbols.

go run market_data/main.go

Exact Public Kline Receipt

Captures one GET /v5/market/kline response with a context and a 4 MiB body limit. No API credentials are needed. Captured body bytes go to stdout without re-encoding or an added newline, while a safe summary and SHA-256 digest go to stderr. Failure responses may still produce partial evidence; inspect the exit status and completeness before using the data.

go run kline_receipt/main.go

This command makes a real public request when run manually. The package tests use only injected transports and local HTTP servers. See the receipt API documentation for bounds, errors, timestamps, and provider-result validation.

Order Management Example

Demonstrates order placement, retrieval, and management.

go run order_management/main.go

Note: This example requires valid API credentials with trading permissions.

WebSocket Public Streams

Shows how to subscribe to public WebSocket streams (orderbook, trades, ticker, klines).

go run websocket_public/main.go

Press Ctrl+C to stop the WebSocket listener.

WebSocket Private Streams

Demonstrates authenticated WebSocket connections for account updates.

go run websocket_private/main.go

Note: Requires valid API credentials.

Press Ctrl+C to stop the WebSocket listener.

Important Notes

  • Demo Trading vs Mainnet: Most examples use demo trading by default. Change Demo: false to use mainnet.
  • API Permissions: Some examples require specific API key permissions (trading, wallet access).
  • Rate Limits: Be mindful of Bybit's API rate limits when running examples repeatedly.
  • WebSocket Examples: These run indefinitely until you press Ctrl+C.

Example Structure

Each example follows this pattern:

  1. Load environment variables
  2. Initialize client/websocket
  3. Perform operations
  4. Display results
  5. Handle errors gracefully

Customization

Feel free to modify these examples to suit your needs. They serve as templates for building your own trading applications.