Ankr provides blockchain RPC services that let developers connect to multiple networks through a unified interface. This guide explains how to obtain and use Ankr RPC endpoints, authenticate requests, and integrate them into applications, with practical examples and considerations for production use.
What Is Ankr Blockchain RPC?
Ankr is a blockchain infrastructure provider that offers Remote Procedure Call (RPC) services for numerous blockchain networks. RPC is the standard protocol that allows applications to interact with blockchains—querying data, sending transactions, and executing smart contracts. Ankr's RPC service abstracts away the complexity of running and maintaining nodes, providing developers with HTTP and WebSocket endpoints to connect to supported chains.
Ankr's RPC offering is part of its broader Web3 infrastructure platform, which also includes staking and other services. For the purpose of this guide, we focus specifically on the RPC endpoints that enable read and write access to blockchains.
- Ankr provides RPC endpoints for many popular blockchains, including Ethereum, Polygon, BNB Chain, and others.
- Endpoints are available via HTTP and WebSocket protocols.
- Access typically requires an API key for authentication, though some public endpoints may exist with rate limits.
How to Access Ankr RPC Endpoints
To use Ankr's RPC service, you generally need to sign up for an account on the Ankr platform and generate an API key. The API key is used to authenticate requests and track usage. Once you have an API key, you can construct endpoint URLs for the networks you need.
Ankr's endpoint URLs follow a pattern that includes the network name and your API key. For example, a typical HTTP endpoint for Ethereum mainnet might look like: https://rpc.ankr.com/eth/YOUR_API_KEY. For other networks, replace 'eth' with the appropriate network identifier, such as 'polygon' for Polygon or 'bsc' for BNB Smart Chain. Always refer to Ankr's official documentation for the most up-to-date list of network identifiers and endpoint formats.
- Sign up for an Ankr account to obtain an API key.
- Construct the endpoint URL using the network identifier and your API key.
- Use the endpoint in your application's RPC configuration.
https://rpc.ankr.com/eth/YOUR_API_KEYAuthentication and Security
Ankr RPC endpoints require authentication via an API key, which is included in the URL path or as a header, depending on the endpoint. It is important to keep your API key secure and not expose it in client-side code or public repositories. For production applications, consider using environment variables or a secrets manager to store the key.
Additionally, Ankr may offer access control features such as whitelisting IP addresses or restricting usage by domain. These features can help prevent unauthorized use of your API key. Always follow best practices for API key management and monitor your usage regularly.
- Never hardcode API keys in client-side code.
- Use environment variables or secure storage for API keys.
- Enable IP whitelisting or other access controls if available.
- Monitor usage to detect unauthorized access.
Supported Networks and Endpoints
Ankr supports a wide range of blockchain networks, including major Layer 1 and Layer 2 chains. The exact list of supported networks may change over time, so it's best to check Ankr's documentation for the current offerings. Common networks include Ethereum, Polygon, BNB Smart Chain, Avalanche, Fantom, and Arbitrum, among others.
Each network has its own endpoint URL structure. For example, Polygon's HTTP endpoint might be https://rpc.ankr.com/polygon/YOUR_API_KEY, while BNB Smart Chain's could be https://rpc.ankr.com/bsc/YOUR_API_KEY. WebSocket endpoints often use a similar pattern with 'wss://' instead of 'https://'.
- Ethereum: https://rpc.ankr.com/eth/YOUR_API_KEY
- Polygon: https://rpc.ankr.com/polygon/YOUR_API_KEY
- BNB Smart Chain: https://rpc.ankr.com/bsc/YOUR_API_KEY
- Avalanche: https://rpc.ankr.com/avalanche/YOUR_API_KEY
- Fantom: https://rpc.ankr.com/fantom/YOUR_API_KEY
Making RPC Requests
Once you have an endpoint, you can interact with the blockchain using standard JSON-RPC methods. For example, to get the latest block number on Ethereum, you would send a POST request with a JSON body containing the method 'eth_blockNumber'. The request should include the 'Content-Type: application/json' header.
Below is an example using curl to call the 'eth_blockNumber' method on an Ankr Ethereum endpoint. Replace YOUR_API_KEY with your actual API key.
- Use standard JSON-RPC methods like eth_blockNumber, eth_getBalance, eth_sendRawTransaction.
- Ensure requests include proper headers and JSON formatting.
- Handle responses and errors according to JSON-RPC specifications.
curl -X POST https://rpc.ankr.com/eth/YOUR_API_KEY \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}'Pricing and Limits
Ankr offers various plans for its RPC services, including free and paid tiers. The free tier typically includes rate limits and may be suitable for development and testing. Paid plans offer higher limits and additional features. Pricing and specific limits are subject to change, so consult Ankr's official website for the most current information.
When planning your integration, consider the expected request volume and choose a plan that accommodates your needs. Be aware of rate limits to avoid unexpected errors during peak usage.
- Free tier available with rate limits.
- Paid plans offer higher throughput and additional features.
- Check Ankr's website for current pricing and limits.
Best Practices for Using Ankr RPC
To ensure reliable and efficient use of Ankr's RPC service, follow these best practices. Implement retry logic with exponential backoff to handle transient errors. Use caching for frequently requested data that doesn't change often. Monitor your usage and set up alerts for rate limit breaches or errors.
For high-availability applications, consider using multiple RPC providers or fallback endpoints to avoid single points of failure. Ankr's service is generally reliable, but no provider is immune to downtime. Diversifying your RPC sources can improve resilience.
- Implement retry logic with exponential backoff.
- Cache immutable or slowly changing data.
- Monitor usage and set up alerts.
- Use multiple RPC providers for failover.
- Keep API keys secure and rotate them periodically.
Conclusion
Ankr provides a convenient and scalable way to access blockchain RPC endpoints across many networks. By following the steps outlined in this guide, developers can quickly integrate Ankr's RPC service into their applications. Remember to secure your API keys, monitor usage, and stay informed about updates to endpoints and pricing by referring to Ankr's official documentation.