Recommended: JavaScript SDK for React Native
While this SDK is still supported, we recommend using newer JavaScript SDK, which is optimized for React Native, but also comes with a host of other benefits.
Looking for a simpler solution? If you’re using Dynamic’s embedded wallets, check out SVM Gas Sponsorship - a built-in feature that automatically sponsors gas fees with just a toggle in the dashboard.
Introduction
In this guide, we’ll show you how to create Solana transactions where a fee payer wallet pays the gas fees instead of your users. We achieve this by partially signing the transaction on the server and sending it to the client for final signing.Getting Started
Setting up the Project
This walkthrough uses a Next.js app (React web SDK) so the UI and fee-payer API routes live in one repo. Create the project withcreate-next-app, then add @dynamic-labs/sdk-react-core and @dynamic-labs/solana with SolanaWalletConnectors in DynamicContextProvider. In the Dynamic dashboard, enable Solana under Chains & Networks and add your dev origin under Security → Allowed Origins. For a native app without Next.js, start from the React Native Quickstart and adapt the API route pattern to your backend.
Setting Up the Fee Payer Wallet
You’ll need a wallet that will pay for gas fees on behalf of your users:- Create a Solana wallet and add some funds to it (for paying gas fees)
- Add its private key (string format) to your
.env.localfile:
.env.local
Never share your private key or commit it to your code repository. Always use environment variables and add them to your
.gitignore.Server Implementation
Creating the API Route
Now we’ll create an API route that will prepare partially-signed transactions. Create a file atapp/api/gas/route.ts:
app/api/gas/route.ts
How It Works
Our API route:- Receives details about the transfer (who’s sending, receiving, and how much)
- Creates a transaction with the instructions
- Sets our server wallet as the fee payer
- Partially signs the transaction with the fee payer wallet
- Returns the transaction to the frontend
Client Implementation
Creating the Frontend Component
Now let’s create a simple UI for users to send tokens without paying gas. Createapp/components/Send.tsx:
app/components/Send.tsx
For styling, create the Send.css file and you can find the styles we used here.
Using the Component
Finally, add the component to your main page inapp/page.tsx:
app/page.tsx