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 { STACKS_MAINNET, STACKS_TESTNET, STACKS_DEVNET } from '@stacks/network'
console.log(STACKS_MAINNET)
{
chainId: 1,
transactionVersion: 0,
peerNetworkId: 385875968,
magicBytes: 'X2',
bootAddress: 'SP000000000000000000002Q6VF78',
addressVersion: { singleSig: 22, multiSig: 20 },
client: { baseUrl: 'https://api.mainnet.hiro.so' }
}
console.log(STACKS_TESTNET)
{
chainId: 2147483648,
transactionVersion: 128,
peerNetworkId: 4278190080,
magicBytes: 'T2',
bootAddress: 'ST000000000000000000002AMW42H',
addressVersion: { singleSig: 26, multiSig: 21 },
client: { baseUrl: 'https://api.testnet.hiro.so' }
}
console.log(STACKS_DEVNET)
{
chainId: 2147483648,
transactionVersion: 128,
peerNetworkId: 4278190080,
magicBytes: 'id',
bootAddress: 'ST000000000000000000002AMW42H',
addressVersion: { singleSig: 26, multiSig: 21 },
client: { baseUrl: 'http://localhost:3999' }
}Custom network configuration
Create a network with an API key:
Create networks with custom node URLs and middleware:
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:
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?