> Feedback: If these docs are stale, missing, or confusing, post sanitized feedback to `https://docs.tempo.xyz/api/feedback` with `source: "mcp"`, a short `message`, and any relevant `toolName`, `relatedResource`, or `client`.
# Go

Tempo distributes a Go SDK for building application clients. The SDK provides packages for RPC communication, transaction signing, and key management.

The Tempo Go SDK can be used to perform common operations with the chain, such as: sending Tempo Transactions, batching multiple calls, fee sponsorship, and more.

::::steps

## Install

To install the Tempo Go SDK:

```bash [go]
go get github.com/tempoxyz/tempo-go@v0.3.0
```

:::tip
The SDK requires Go 1.21 or higher.
:::

## Create an RPC Client

To interact with Tempo, first create an RPC client connected to a Tempo node:

```go [main.go]
package main

import (
    "context"
    "fmt"

    "github.com/tempoxyz/tempo-go/pkg/client"
)

func main() {
    c := client.New("https://rpc.tempo.xyz") 

    ctx := context.Background()
    blockNum, _ := c.GetBlockNumber(ctx)
    fmt.Printf("Connected to Tempo at block %d\n", blockNum)
}
```

For authenticated RPC endpoints:

```go [main.go]
c := client.New("https://rpc.tempo.xyz",
    client.WithAuth("username", "password"),
)
```

## Create a Signer

Create a signer to sign transactions. The signer manages your private key and generates signatures:

```go [main.go]
package main

import (
    "fmt"

    "github.com/tempoxyz/tempo-go/pkg/signer"
)

func main() {
    s, err := signer.NewSigner("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
    if err != nil {
        panic(err)
    }

    fmt.Printf("Address: %s\n", s.Address().Hex())
}
```

## Send a Transaction

Build and send a transaction using the builder pattern:

```go [main.go]
package main

import (
    "context"
    "log"
    "math/big"

    "github.com/ethereum/go-ethereum/common"
    "github.com/tempoxyz/tempo-go/pkg/client"
    "github.com/tempoxyz/tempo-go/pkg/signer"
    "github.com/tempoxyz/tempo-go/pkg/transaction"
)

func main() {
    c := client.New("https://rpc.tempo.xyz")
    s, _ := signer.NewSigner("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")

    ctx := context.Background()
    nonce, _ := c.GetTransactionCount(ctx, s.Address().Hex()) // [!code hl]

    recipient := common.HexToAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8")

    // [!code hl:10]
    tx := transaction.NewBuilder(big.NewInt(4217)). // Tempo mainnet
        SetNonce(nonce).
        SetGas(100000).
        SetMaxFeePerGas(big.NewInt(20000000000)). // 20 gwei base fee
        SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
        AddCall(recipient, big.NewInt(0), []byte{}).
        Build()

    transaction.SignTransaction(tx, s)
    serialized, _ := transaction.Serialize(tx, nil)
    hash, _ := c.SendRawTransaction(ctx, serialized) // [!code hl]
    log.Printf("Transaction hash: %s", hash)
}
```

::::

## Examples

### Read Chain Data

Query the blockchain for basic information:

```go [read.go]
ctx := context.Background()

blockNum, _ := c.GetBlockNumber(ctx)
chainID, _ := c.GetChainID(ctx)
nonce, _ := c.GetTransactionCount(ctx, "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb")

fmt.Printf("Block: %d, Chain: %d, Nonce: %d\n", blockNum, chainID, nonce)
```

### Token Transfer

Send a TIP-20 token transfer using go-ethereum's ABI encoding:

```go [transfer.go]
import "github.com/ethereum/go-ethereum/accounts/abi"

erc20ABI, _ := abi.JSON(strings.NewReader(`[{"name":"transfer","type":"function","inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}]}]`))

recipient := common.HexToAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8")
amount := big.NewInt(100_000_000) // 100 tokens (6 decimals)

transferData, _ := erc20ABI.Pack("transfer", recipient, amount)

tx := transaction.NewBuilder(big.NewInt(4217)).
    SetNonce(nonce).
    SetGas(100000).
    SetMaxFeePerGas(big.NewInt(10000000000)).
    SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
    AddCall(transaction.AlphaUSDAddress, big.NewInt(0), transferData).
    Build()
```

### Transfer with Memo

Include a memo for payment reconciliation:

```go [memo.go]
tip20ABI, _ := abi.JSON(strings.NewReader(`[{"name":"transferWithMemo","type":"function","inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"},{"name":"memo","type":"bytes32"}]}]`))

recipient := common.HexToAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8")
amount := big.NewInt(100_000_000)
memo := [32]byte{}
copy(memo[:], "INV-12345")

memoData, _ := tip20ABI.Pack("transferWithMemo", recipient, amount, memo)

tx := transaction.NewBuilder(big.NewInt(4217)).
    SetNonce(nonce).
    SetGas(100000).
    SetMaxFeePerGas(big.NewInt(10000000000)).
    SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
    AddCall(transaction.AlphaUSDAddress, big.NewInt(0), memoData).
    Build()
```

### Batch Multiple Calls

Execute multiple operations atomically in a single transaction:

```go [batch.go]
tx := transaction.NewBuilder(big.NewInt(4217)).
    SetNonce(nonce).
    SetGas(200000).
    SetMaxFeePerGas(big.NewInt(10000000000)).
    SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
    AddCall(addr1, big.NewInt(0), transfer1Data). // [!code hl]
    AddCall(addr2, big.NewInt(0), transfer2Data). // [!code hl]
    AddCall(addr3, big.NewInt(0), contractCallData). // [!code hl]
    Build()

transaction.SignTransaction(tx, s)
```

### Parallel Transactions (2D Nonces)

Send multiple transactions concurrently using different nonce keys:

```go [parallel.go]
tx1 := transaction.NewBuilder(big.NewInt(4217)).
    SetNonceKey(big.NewInt(1)). // Sequence A // [!code hl]
    SetNonce(0).
    SetGas(100000).
    SetMaxFeePerGas(big.NewInt(10000000000)).
    SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
    AddCall(recipient1, big.NewInt(0), data1).
    Build()

tx2 := transaction.NewBuilder(big.NewInt(4217)).
    SetNonceKey(big.NewInt(2)). // Sequence B (parallel) // [!code hl]
    SetNonce(0).
    SetGas(100000).
    SetMaxFeePerGas(big.NewInt(10000000000)).
    SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
    AddCall(recipient2, big.NewInt(0), data2).
    Build()

transaction.SignTransaction(tx1, s)
transaction.SignTransaction(tx2, s)

// Send both in parallel
go func() { c.SendRawTransaction(ctx, serialize(tx1)) }()
go func() { c.SendRawTransaction(ctx, serialize(tx2)) }()
```

### Fee Sponsorship

Have another account pay for transaction fees:

```go [feepayer.go]
tx := transaction.NewBuilder(big.NewInt(4217)).
    SetNonce(nonce).
    SetGas(100000).
    SetMaxFeePerGas(big.NewInt(10000000000)).
    SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
    SetSponsored(true). // Mark as awaiting fee payer // [!code hl]
    AddCall(recipient, big.NewInt(0), data).
    Build()

transaction.SignTransaction(tx, userSigner)
transaction.AddFeePayerSignature(tx, feePayerSigner)
```

### Transaction Validity Window

Set a time window during which the transaction is valid:

```go [validity.go]
now := time.Now()

tx := transaction.NewBuilder(big.NewInt(4217)).
    SetNonce(nonce).
    SetGas(100000).
    SetMaxFeePerGas(big.NewInt(10000000000)).
    SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
    SetValidAfter(uint64(now.Unix())). // [!code hl]
    SetValidBefore(uint64(now.Add(1 * time.Hour).Unix())). // [!code hl]
    AddCall(recipient, big.NewInt(0), data).
    Build()
```

### Batch RPC Requests

Send multiple RPC calls efficiently in a single HTTP request:

```go [batch_rpc.go]
batch := client.NewBatchRequest()
batch.Add("eth_blockNumber").
    Add("eth_chainId").
    Add("eth_getBalance", "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb", "latest")

responses, _ := c.SendBatch(ctx, batch)
for _, resp := range responses {
    fmt.Printf("Result: %v\n", resp.Result)
}
```

## Account Keychain

The `keychain` package provides typed helpers for Tempo's [Account Keychain precompile](/docs/protocol/transactions/AccountKeychain), enabling access key management and signing directly from Go.

:::info
Enhanced access key features — periodic spending limits and call scoping — require the [T3 network upgrade](/docs/protocol/upgrades/t3).
:::

```go [keychain_manage.go]
package main

import (
	"context"
	"math/big"
	"time"

	"github.com/ethereum/go-ethereum/common"
	"github.com/tempoxyz/tempo-go/pkg/client"
	"github.com/tempoxyz/tempo-go/pkg/keychain"
	"github.com/tempoxyz/tempo-go/pkg/signer"
	"github.com/tempoxyz/tempo-go/pkg/transaction"
)

func main() {
	c := client.New("https://rpc.tempo.xyz")
	s, _ := signer.NewSigner("0xYOUR_PRIVATE_KEY")

	ctx := context.Background()
	nonce, _ := c.GetTransactionCount(ctx, s.Address().Hex())

	accessKeyAddr := common.HexToAddress("<ACCESS_KEY_ADDRESS>")

	// Authorize a new access key (secp256k1, no expiry):
	restrictions := keychain.NewKeyRestrictions(0)
	call, _ := keychain.AuthorizeKey(accessKeyAddr, keychain.SignatureTypeSecp256k1, restrictions)

	tx := transaction.NewBuilder(big.NewInt(4217)).
		SetNonce(nonce).
		SetGas(200000).
		SetMaxFeePerGas(big.NewInt(20000000000)).
		SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
		AddCall(call.To, big.NewInt(0), call.Data).
		Build()

	// Authorize with a spending limit:
	token := common.HexToAddress("<TOKEN_ADDRESS>")
	restrictions = keychain.NewKeyRestrictions(0).
		WithLimits([]keychain.TokenLimit{{Token: token, Amount: big.NewInt(1_000_000)}})
	call, _ = keychain.AuthorizeKey(accessKeyAddr, keychain.SignatureTypeSecp256k1, restrictions)

	// Authorize with call scopes (restrict to specific contracts/functions):
	scope := keychain.NewCallScopeBuilder(token).
		Transfer(nil).
		Approve(nil).
		Build()
	restrictions = keychain.NewKeyRestrictions(0).
		WithAllowedCalls([]keychain.CallScope{scope})
	call, _ = keychain.AuthorizeKey(accessKeyAddr, keychain.SignatureTypeSecp256k1, restrictions)

	// Full example: 24h expiry + spending limit + call scope:
	expiry := uint64(time.Now().Add(24 * time.Hour).Unix())
	restrictions = keychain.NewKeyRestrictions(expiry).
		WithLimits([]keychain.TokenLimit{{Token: token, Amount: big.NewInt(1_000_000)}}).
		WithAllowedCalls([]keychain.CallScope{
			keychain.NewCallScopeBuilder(token).Transfer(nil).Build(),
		})
	call, _ = keychain.AuthorizeKey(accessKeyAddr, keychain.SignatureTypeSecp256k1, restrictions)

	// Revoke an access key (permanent, cannot be re-authorized):
	call, _ = keychain.RevokeKey(accessKeyAddr)

	// Update spending limit for a key-token pair:
	call, _ = keychain.UpdateSpendingLimit(accessKeyAddr, token, big.NewInt(2_000_000))

	// Replace all call scopes for a key:
	call, _ = keychain.SetAllowedCalls(accessKeyAddr, []keychain.CallScope{
		keychain.NewCallScopeBuilder(token).Transfer(nil).Build(),
	})

	// Remove a target contract from allowed call list:
	call, _ = keychain.RemoveAllowedCalls(accessKeyAddr, token)

	_ = ctx
	_ = tx
	_ = call
}
```

### Signing with an Access Key

Use `keychain.SignWithAccessKey` to sign a transaction as an access key holder:

```go [access_key_sign.go]
accessKeySigner, _ := signer.NewSigner("<ACCESS_KEY_PRIVATE_KEY>")
rootAccount := common.HexToAddress("<ROOT_ACCOUNT_ADDRESS>")

tx := transaction.NewBuilder(big.NewInt(4217)).
	SetNonce(nonce).
	SetGas(100000).
	SetMaxFeePerGas(big.NewInt(20000000000)).
	SetMaxPriorityFeePerGas(big.NewInt(1000000000)).
	AddCall(recipient, big.NewInt(0), data).
	Build()

keychain.SignWithAccessKey(tx, accessKeySigner, rootAccount) // [!code hl]
serialized, _ := transaction.Serialize(tx, nil)
hash, _ := c.SendRawTransaction(ctx, serialized)
```

### Query Remaining Spending Limit

```go [query_limit.go]
calldata := keychain.EncodeGetRemainingLimitCalldata(
	common.HexToAddress("<ACCOUNT_ADDRESS>"),
	common.HexToAddress("<ACCESS_KEY_ADDRESS>"),
	common.HexToAddress("<TOKEN_ADDRESS>"),
)
result, _ := c.Call(ctx, keychain.GetKeychainAddress().Hex(), calldata)
remaining := keychain.ParseRemainingLimitResult(result)
fmt.Printf("Remaining: %s\n", remaining.String())
```

## Packages

| Package | Description |
| --- | --- |
| `transaction` | TempoTransaction encoding, signing, and validation |
| `client` | RPC client for interacting with Tempo nodes |
| `signer` | Key management and signature generation |
| `keychain` | Account Keychain precompile: access key management and signing |

## Next Steps

After setting up the Go SDK, you can:

* Follow a guide on how to [make payments](/docs/guide/payments), [issue stablecoins](/docs/guide/issuance), [exchange stablecoins](/docs/guide/stablecoin-dex), and [more](/docs).
* View the [examples on GitHub](https://github.com/tempoxyz/tempo-go/tree/main/examples)
