# Create a sponsored tx

{% code fullWidth="false" expandable="true" %}

```typescript
import { bytesToHex } from "@stacks/common";
import {
  broadcastTransaction,
  deserializeTransaction,
  makeContractCall,
  sponsorTransaction,
  BufferReader,
  AnchorMode,
  Cl,
} from "@stacks/transactions";

// Step 1: User creates the transaction with sponsored flag
const userTxOptions = {
  contractAddress: "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM",
  contractName: "my-contract",
  functionName: "my-function",
  functionArgs: [Cl.uint(123)],
  fee: 0, // User doesn't pay fees
  senderKey: "b244296d5907de9864c0b0d51f98a13c52890be0404e83f273144cd5b9960eed01",
  network: 'testnet',
  sponsored: true, // Mark as sponsored
};

const transaction = await makeContractCall(userTxOptions);
const serializedTx = bytesToHex(transaction.serialize());

// Step 2: Send serialized transaction to sponsor
// (In practice, this would be sent to a sponsorship service)

// Step 3: Sponsor signs and pays fees
const sponsorKey = "753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601";
const deserializedTx = deserializeTransaction(serializedTx);
const sponsoredTx = await sponsorTransaction({
  transaction: deserializedTx,
  sponsorPrivateKey: sponsorKey,
  fee: 1000, // Sponsor pays the fee
  sponsorNonce: 0,
});

// Step 4: Broadcast the sponsored transaction
const broadcastResponse = await broadcastTransaction({
  transaction: sponsoredTx,
  network: STACKS_TESTNET,
});

console.log("Sponsored transaction ID:", broadcastResponse.txid);
```

{% endcode %}

### Description

Build transactions where a sponsor pays the fees on behalf of users

### Use Cases

* Onboarding new users without STX for fees
* Subsidizing transaction costs for dApp users
* Enterprise applications paying for user transactions
* Gaming applications with seamless user experience

### Key Concepts

* **User** - Creates and signs the transaction with `sponsored: true`
* **Sponsor** - Pays the fees and broadcasts the transaction
* Sponsored transactions have two parties


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.stacks.co/cookbook/stacks.js/transaction-building/create-a-sponsored-tx.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
