Network Configuration
Configure and customize Stacks network connections.
Overview
Stacks.js supports multiple networks—mainnet for production, testnet for development, and custom networks for local testing. Proper network configuration ensures your app connects to the right blockchain instance with optimal settings.
Basic network setup
Configure standard networks:
import {
StacksMainnet,
StacksTestnet,
StacksMocknet
} from '@stacks/network';
// Production network
const mainnet = new StacksMainnet();
console.log('Mainnet API:', mainnet.coreApiUrl);
// https://api.hiro.so
// Test network
const testnet = new StacksTestnet();
console.log('Testnet API:', testnet.coreApiUrl);
// https://api.testnet.hiro.so
// Local development network
const mocknet = new StacksMocknet();
console.log('Mocknet API:', mocknet.coreApiUrl);
// http://localhost:3999Custom network configuration
Create networks with custom endpoints:
Environment-based configuration
Manage networks across environments:
Network detection and validation
Detect and validate network connections:
Advanced network features
Custom headers and authentication
Add authentication or custom headers:
Request retry and timeout
Implement resilient network requests:
Network-specific configurations
Configure by chain ID
Set up network based on chain identifier:
Multi-network support
Support multiple networks simultaneously:
Network connection monitoring
Monitor network health and status:
WebSocket configuration
Set up real-time connections:
Testing with different networks
Set up tests across networks:
Best practices
Use environment variables: Never hardcode network URLs
Implement retry logic: Networks can be temporarily unavailable
Monitor connection health: Detect and handle network issues
Cache network info: Reduce redundant API calls
Validate network type: Ensure you're on the expected network
Common issues
Last updated
Was this helpful?
