> ## Documentation Index
> Fetch the complete documentation index at: https://docs.dynamic.xyz/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# sponsorTransaction

> Sponsors a Solana transaction via the Dynamic gas sponsorship API

## Function Signature

```typescript theme={"system"}
sponsorTransaction(params: {
  transaction: VersionedTransaction | Transaction;
  userId?: string;
  traceContext?: TraceContext;
}): Promise<VersionedTransaction>
```

## Description

Sends an unsigned Solana transaction to Dynamic's gas sponsorship endpoint (`/solana/sponsorTransaction`), which replaces the fee payer with the sponsor's address. The returned transaction has the sponsor as fee payer and should be passed to [`signTransaction()`](https://docs.dynamic.xyz/docs/node/reference/svm/sign-transaction) instead of the original.

Gas sponsorship must be enabled for your environment in the Dynamic Console before calling this method.

> [!NOTE]
>
> For most use cases, pass `sponsor: true` to [`signTransaction()`](https://docs.dynamic.xyz/docs/node/reference/svm/sign-transaction) instead of calling `sponsorTransaction()` directly. Use this method when you need to inspect or modify the sponsored transaction before signing.

## Parameters

### Required Parameters

* **`transaction`** (`VersionedTransaction | Transaction`) — The unsigned Solana transaction. Supports both legacy `Transaction` and `VersionedTransaction`.

### Optional Parameters

* **`userId`** (`string`) — The end user the transaction is for. Required when the signing wallet is that user's wallet rather than one of your own server wallets. The signing wallet must belong to this user in your environment, and the request is rejected otherwise. Omit it to sponsor transactions signed by your own server wallets. Requires `@dynamic-labs-wallet/node-svm` version 1.1.64 or later.
* **`traceContext`** (`TraceContext`) — Distributed tracing context for observability.

## Returns

**`Promise<VersionedTransaction>`** — The sponsored transaction with the gas sponsor set as fee payer and the sponsor's signature in place. The endpoint always returns a versioned transaction, whichever type you pass in.

## Example

```typescript theme={"system"}
import { DynamicSvmWalletClient } from '@dynamic-labs-wallet/node-svm';
import { Connection, Transaction, SystemProgram, PublicKey } from '@solana/web3.js';

const svmClient = await authenticatedSvmClient();
const connection = new Connection(process.env.SOLANA_RPC_URL ?? 'https://api.mainnet-beta.solana.com');

const { blockhash } = await connection.getLatestBlockhash();

const transaction = new Transaction().add(
  SystemProgram.transfer({
    fromPubkey: new PublicKey(walletMetadata.accountAddress),
    toPubkey: new PublicKey(recipientAddress),
    lamports: 1000000,
  })
);
transaction.recentBlockhash = blockhash;
transaction.feePayer = new PublicKey(walletMetadata.accountAddress);

// Sponsor replaces the fee payer
const sponsoredTx = await svmClient.sponsorTransaction({ transaction });

// Inspect or modify the sponsored transaction, then sign
const signatureBase58 = await svmClient.signTransaction({
  walletMetadata,
  externalServerKeyShares,
  transaction: sponsoredTx,
});
```

## Sponsor for an end user

To sponsor a transaction signed by an end user's wallet, pass that user's `userId`. When your environment restricts sponsorship to server-side requests, this is how your backend sponsors transactions for users. The [SVM Server-Controlled Sponsorship](https://docs.dynamic.xyz/docs/recipes/integrations/svm-server-controlled-sponsorship.md) recipe shows the full flow.

```typescript theme={"system"}
const sponsoredTx = await svmClient.sponsorTransaction({ transaction, userId });
```

## Error Handling

The method throws when sponsorship is unavailable or not enabled:

```typescript theme={"system"}
try {
  const sponsoredTx = await svmClient.sponsorTransaction({ transaction });
} catch (error) {
  if (error.message.includes('400')) {
    console.error('Gas sponsorship is not enabled for this environment');
  } else {
    console.error('Sponsorship failed:', error.message);
  }
}
```

## Related

* [`signTransaction()`](https://docs.dynamic.xyz/docs/node/reference/svm/sign-transaction) - Sign a transaction (accepts `sponsor: true` for a one-step flow)
* [`getSponsorAddress()`](https://docs.dynamic.xyz/docs/node/reference/svm/get-sponsor-address) - Read the sponsor address to build transactions that name it as fee payer
* [SVM Server-Controlled Sponsorship](https://docs.dynamic.xyz/docs/recipes/integrations/svm-server-controlled-sponsorship.md) - Build and validate the transaction on your backend, then sponsor it for a user
* [SVM Gas Sponsorship guide](https://docs.dynamic.xyz/docs/node/wallets/server-wallets/gas-sponsorship-svm.md) - Full guide on SVM gas sponsorship approaches


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.