Transactions

Distribute Tokens

The transactions API makes it effortless for any developer to airdrop tokens at scale. Gas is fully sponsored, you only need a recipient address!

Airdrop overview

Prerequisites

  • A deployed token contract (Coin or NFT contract)
  • A server wallet holding tokens to distribute
  • A client ID and secret key from your Team > Project > Settings page.

Frontend: Add Connect Wallet and Claim buttons

Use ConnectButton component to prompt the user for their wallet. The Claim button calls POST /api/claim.

import { createThirdwebClient } from "thirdweb";
import {
ThirdwebProvider,
ConnectButton,
useActiveAccount,
} from "thirdweb/react";
const client = createThirdwebClient({
clientId: "your-client-id",
});
function ClaimPage() {
const account = useActiveAccount();
const address = account?.address;
const onClick = async () => {
await fetch("/api/claim", {
method: "POST",
body: JSON.stringify({ address }),
});
alert(`🎉 A reward has been sent to your wallet: ${address}`);
};
return (
<main>
<h2>Thank you for being a superfan! ❤️</h2>
<ConnectButton client={client} />
{address && <button onClick={onClick}>Claim my reward</button>}
</main>
);
}
function Example() {
return (
<ThirdwebProvider>
<ClaimPage />
</ThirdwebProvider>
);
}

Backend: Call the transactions API

POST /api/claim calls the transactions API to send tokens to the user's wallet.

export async function POST(request: Request) {
const { userWalletAddress } = await request.json();
await fetch("https://engine.thirdweb.com/v1/contract/write", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-secret-key": "<thirdweb_secret_key>",
},
body: JSON.stringify({
executionOptions: {
from: "<server_wallet_address>",
chainId: "<chain_id>",
},
params: [
{
contractAddress: "<token_contract_address>",
method: "function transfer(address to, uint256 amount)",
params: [userWalletAddress, 1000000000000000000], // 1 token
},
],
}),
});
return NextResponse.json({ message: "Success!" });
}

Try it out!

Here’s what the user flow looks like.

The app prompts the user to connect their wallet.

Initial page load
The app prompts the user to connect their wallet

A user presses claim.

A user presses claim

They'll receive the tokens in their wallet shortly!

They'll receive the tokens in their wallet shortly