Gas Stations
Gas stations are a powerful resource that allows you to subsidize transaction fees for your users. This means your users can interact with your dApp without needing to hold APT tokens for gas fees, making the user experience much smoother.
How It Works
A gas station acts as a fee payer for transactions on the Aptos blockchain. When properly configured:
- Your users submit transactions using your dApp
- The gas station automatically pays the gas fees
- Your users don’t need to hold APT tokens
- You maintain control over which transactions can be subsidized
Creating a Gas Station
To create a gas station:
- Navigate to your project page
- Add a “Gas Station” resource
- Provide a name and select a network
- Configure the initial contract rules
Each gas station requires an API key for authentication. One will be automatically created for you when you create the gas station.
Integration
This guide explains how to use the Aptos Build Gas Station in a Typescript based dapp.
Install the relevant packages:
pnpm add \
@aptos-labs/ts-sdk@^3.1.1 \
@aptos-labs/gas-station-client@^1.1.1 \
@aptos-labs/wallet-adapter-core@^6.0.0 \
@aptos-labs/wallet-adapter-react@^7.0.0
Build a gas station client and configure the Aptos
client to use it to submit transactions:
import { AptosGasStationClient } from "@aptos-labs/gas-station-client";
const network = Network.MAINNET;
const gasStationClient = createGasStationClient({
network,
apiKey: "aptoslabs_34edbqbxyPrS_6fu9i83mYmjid4MVs3Z3XmCeATQ8tkSda",
});
const config = new AptosConfig({
network,
pluginSettings: {
TRANSACTION_SUBMITTER: gasStationClient,
},
});
const aptos = new Aptos(config);
If you’re using the wallet adapter, you can configure it to use the gas station client too:
import { AptosWalletAdapterProvider } from "@aptos-labs/wallet-adapter-react";
<AptosWalletAdapterProvider
dappConfig={{
network: state.network,
transactionSubmitter: aptos.config.getTransactionSubmitter(),
}}
autoConnect={true}
>
<MyApp />
</AptosWalletAdapterProvider>
From this point on, any transaction you submit with the TS SDK client or via the wallet adapter (e.g. with signAndSubmitTransaction
from useWallet
) will be submitted via the gas station.
If you have configured a reCAPTCHA check, you can provide the token as part of submitting the transaction like this:
import { useWallet } from "@aptos-labs/wallet-adapter-react";
const { signAndSubmitTransaction } = useWallet();
// Make the user do a reCAPTCHA check.
const recaptchaToken = await getRecaptchaToken();
// Build and submit the transaction with the reCAPTCHA token. This will sign locally
// and then submit to the gas station.
const response = await signAndSubmitTransaction({
data: {
function: "0x1::coin::transfer",
typeArguments: [APTOS_COIN],
functionArguments: ["0x9", 100_000_000],
},
withFeePayer: true,
pluginParams: { recaptchaToken },
});
Learn more about API keys and authentication here.
Contract Rules
Contract rules define which transactions can be subsidized by your gas station. For each contract, you can:
- Specify which functions can be called
- Set spending limits per function
- Enable reCAPTCHA protection
- Configure rate limits
Always carefully review and test your contract rules before deploying to production. Incorrect rules could lead to unexpected gas costs.
Billing
Gas stations are billed based on the actual gas costs of the transactions they subsidize. You can:
- Monitor usage in real-time
- Set up alerts for high usage
- View detailed billing reports
- Configure spending limits
Learn more about billing and costs here (see “Gas” in the Cost Dimensions section).
Security Features
Gas stations include several security features to protect your resources:
- Function-level Control: Only allow specific functions to be called
- Spending Limits: Set maximum gas costs per function
- Rate Limiting: Prevent abuse through request throttling
- reCAPTCHA: Optional protection against automated attacks
- API Key Authentication: Secure access to your gas station
Limitations
- Gas stations can only subsidize transactions on the specified network
- Each function call must be explicitly allowed in the contract rules
- There are rate limits and spending limits to prevent abuse
- Some complex transactions may not be supported