This documentation is using @aptos-labs/ts-sdk v6.0.0
API Keys with TS SDK
Learn how to use API keys with the Aptos TS SDK to improve rate limits and reliability.
Why Use API Keys?
API keys provide several benefits:
- Improved rate limits: Higher request limits for authenticated requests
- Better reliability: More stable API access
- Enhanced monitoring: Track your API usage
- Production ready: Essential for production applications
Getting an API Key
To get an API key:
- Visit Geomi to register for a free account
- Navigate to the API Keys section in your dashboard
- Create a new API key
For detailed instructions on setting up API keys with Geomi, see the Geomi API Keys documentation. You can also check our Getting an API Key guide for step-by-step instructions.
Configuring API Keys
Basic Configuration
Pass your API key when creating the Aptos client:
export function basicApiKeyConfiguration() {
const aptos = new Aptos(
new AptosConfig({
network: Network.MAINNET,
clientConfig: {
API_KEY: "your-api-key-here",
},
}),
);
return aptos;
}Using Environment Variables
Recommended: Store your API key in an environment variable:
# .env
APTOS_NAMES_API_KEY=your-api-key-here
Then use it in your code:
export function apiKeyFromEnvironment() {
const API_KEY = process.env.APTOS_NAMES_API_KEY;
if (!API_KEY) {
throw new Error("APTOS_NAMES_API_KEY environment variable is required");
}
const aptos = new Aptos(
new AptosConfig({
network: Network.MAINNET,
clientConfig: {
API_KEY,
},
}),
);
return aptos;
}Using API Keys
Once configured, the API key is automatically used for all requests:
export async function usingApiKey() {
const aptos = new Aptos(
new AptosConfig({
network: Network.MAINNET,
clientConfig: {
API_KEY: process.env.APTOS_NAMES_API_KEY,
},
}),
);
// All requests will use the API key automatically
const name = await aptos.getName({ name: "example.apt" });
const primaryName = await aptos.getPrimaryName({ address: "0x123..." });
const names = await aptos.getAccountNames({ accountAddress: "0x123..." });
return { name, primaryName, names };
}Multiple Environments
Use different API keys for different environments:
export function createAptosClient(network: Network) {
const apiKey =
network === Network.MAINNET
? process.env.APTOS_NAMES_API_KEY_MAINNET
: process.env.APTOS_NAMES_API_KEY_TESTNET;
return new Aptos(
new AptosConfig({
network,
clientConfig: apiKey ? { API_KEY: apiKey } : undefined,
}),
);
}