> For the complete documentation index, see [llms.txt](https://docs.hyperbeat.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hyperbeat.org/hyperbeat-staking/staking-api/sign-and-broadcast-transaction.md).

# Sign and Broadcast Transaction

To sign and broadcast a transaction to the Hyperliquid network, follow these steps:

1. Retrieve an unsigned transaction (`unsignedTransaction`) from of one of the following requests responses: [transfer hype from spot to staking balance](https://docs.p2p.org/reference/hyperliquid-transfer), [delegate](https://docs.p2p.org/reference/hyperliquid-delegate), [undelegate](https://docs.p2p.org/reference/hyperliquid-undelegate) and [withdraw](https://docs.p2p.org/reference/hyperliquid-withdraw).
2. Sign the transaction using the following code:

```javascript
import { signUserSignedAction, userSignedActionEip712Types, isValidPrivateKey } from "@nktkas/hyperliquid/signing";

async function signTransaction(privateKey, unsignedTransaction) {
  const action = unsignedTransaction.action;

  return Buffer.from(
    JSON.stringify({
      ...unsignedTransaction,
      signature: await signUserSignedAction({
        wallet: privateKey,
        action: action,
        types: userSignedActionEip712Types[action.type],
      }),
    })
  ).toString("base64");
}

(async () => {
  const [, , rawTransaction, privateKey] = process.argv;

  if (!rawTransaction || typeof rawTransaction !== "string") {
    console.error("Invalid raw transaction. It should be a base64-encoded string.");
    process.exit(1);
  }

  if (!privateKey || typeof privateKey !== "string" || !isValidPrivateKey(privateKey)) {
    console.error("Invalid private key.");
    process.exit(1);
  }

  const unsignedTransaction = JSON.parse(Buffer.from(rawTransaction, "base64").toString("utf-8"));

  if (
    !("action" in unsignedTransaction) ||
    !("type" in unsignedTransaction.action) ||
    !["cDeposit", "tokenDelegate", "cWithdraw"].includes(unsignedTransaction.action.type)
  ) {
    console.error("Invalid unsigned transaction.");
    process.exit(1);
  }

  console.log(await signTransaction(privateKey.startsWith("0x") ? privateKey : `0x${privateKey}`, unsignedTransaction));
})();

/**
 *  To run with env variables:
 *
 *  $ export RAW_TX= 'rawtxhere'
 *  $ export PRIVATE_KEY= 'privatekeyhere'
 *  $ npx --yes -p @nktkas/hyperliquid@0.23.1 node hyperliquid-signer.js $RAW_TX $PRIVATE_KEY
 **/
```

3. Send the signed transaction using the [Send a transaction to the network](https://docs.p2p.org/reference/hyperliquid-send) endpoint.<br>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.hyperbeat.org/hyperbeat-staking/staking-api/sign-and-broadcast-transaction.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
