# Transactions with off-chain signatures

This guide shows how to interact with the Safe Transaction Service API to create, sign, and execute transactions with the owners of a Safe account.

The different steps are implemented using [Curl](https://github.com/curl/curl) requests, the [Safe\{Core\} SDK](https://github.com/safe-global/safe-core-sdk) TypeScript library and the [safe-eth-py](https://github.com/safe-global/safe-eth-py) Python library.

The [Safe Queue Service](../safe-queue-service-overview.mdx) serves the endpoints to propose transactions and collect their signatures for every chain, so the Curl requests in this guide call it. The request that creates a transaction passes the chain ID in the body. If your integration still calls the Safe Transaction Service for these endpoints, see the [migration guide](../safe-queue-service-migration.mdx).

## Prerequisites

1. [Node.js and npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm#using-a-node-version-manager-to-install-nodejs-and-npm) when using the Safe\{Core\} SDK.
2. [Python](https://www.python.org/downloads/) >= 3.9 when using `safe-eth-py`.
3. Have a Safe account configured with a threshold of 2, where two signatures are needed.

## Steps

<Steps>
  ### Install dependencies


  <Tabs items={['TypeScript', 'Python']}>
    <Tabs.Tab>
      ```bash
      yarn add @safe-global/api-kit @safe-global/protocol-kit @safe-global/types-kit
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```bash
      pip install safe-eth-py web3 hexbytes
      ```
    </Tabs.Tab>
  </Tabs>


  ### Imports


  <Tabs items={['TypeScript', 'Python']}>
    <Tabs.Tab>
      ```typescript
      import SafeApiKit from '@safe-global/api-kit'
      import Safe from '@safe-global/protocol-kit'
      import {
        MetaTransactionData,
        OperationType
      } from '@safe-global/types-kit'
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```python
      from safe_eth.eth import EthereumClient, EthereumNetwork
      from safe_eth.safe.api.transaction_service_api import TransactionServiceApi
      from safe_eth.safe import Safe
      from hexbytes import HexBytes
      ```
    </Tabs.Tab>
  </Tabs>


  ### Create a Safe transaction


  <Tabs items={['TypeScript', 'Python', 'Curl']}>
    <Tabs.Tab>
      ```typescript
      // Initialize the Protocol Kit with Owner A
      const protocolKitOwnerA = await Safe.init({
        provider: config.RPC_URL,
        signer: config.OWNER_A_PRIVATE_KEY,
        safeAddress: config.SAFE_ADDRESS
      })

      // Create a Safe transaction
      const safeTransactionData: MetaTransactionData = {
        to: config.TO,
        value: config.VALUE,
        data: '0x',
        operation: OperationType.Call
      }

      const safeTransaction = await protocolKitOwnerA.createTransaction({
        transactions: [safeTransactionData]
      })
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```python
      ethereum_client = EthereumClient(config.get("RPC_URL"))

      # Instantiate a Safe
      safe = Safe(config.get("SAFE_ADDRESS"), ethereum_client)

      # Create a Safe transaction
      safe_tx = safe.build_multisig_tx(
          config.get("TO"),
          config.get("VALUE"),
          HexBytes(""))
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```bash
      curl -X 'POST' \
      'https://api.safe.global/queue/api/v1/multisig-transactions' \
      -H 'accept: application/json' \
      -H 'Content-Type: application/json' \
      -H 'Authorization: Bearer YOUR_API_KEY' \
      -d '{
          "chainId": 11155111,
          "safe": "0xc62C5cbB964459F3C984682f78A4d3ffffffffff",
          "to": "0x795D6C88B4Ea3CCffffffffffCa8a11Bc0496228",
          "value": 2000000000000000,
          "data": "0x",
          "operation": 0,
          "gasToken": "0x0000000000000000000000000000000000000000",
          "safeTxGas": 0,
          "baseGas": 0,
          "gasPrice": 0,
          "refundReceiver": "0x0000000000000000000000000000000000000000",
          "nonce": 15,
          "signatures": [
              "0x6a2b57023af16241511619ea95f7cd03d00aa6b79d1ca80e21a0b89cd2c38ffffffffff9b738ffbd680c4d717b9b0c9eae568f3edebc40a0c004700bffffffffff"
          ]
      }'
      ```
    </Tabs.Tab>
  </Tabs>


  ### Sign the transaction


  <Tabs items={['TypeScript', 'Python', 'Curl']}>
    <Tabs.Tab>
      ```typescript
      // Sign the transaction with Owner A
      const safeTxHash = await protocolKitOwnerA.getTransactionHash(safeTransaction)
      const signatureOwnerA = await protocolKitOwnerA.signHash(safeTxHash)
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```python
      # Sign the transaction with Owner A
      safe_tx.sign(config.get("OWNER_A_PRIVATE_KEY"))
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      We skip this step because the transaction we created in the Safe Queue Service using Curl already has the signature of the transaction creator. Check the [Create a Safe transaction](#create-a-safe-transaction) step.
    </Tabs.Tab>
  </Tabs>


  ### Send the transaction to the service


  <Tabs items={['TypeScript', 'Python', 'Curl']}>
    <Tabs.Tab>
      ```typescript
      // Initialize the API Kit
      // How to get an Api key => http://docs.safe.global/core-api/how-to-use-api-keys
      const apiKit = new SafeApiKit({
        chainId: 11155111n,
        apiKey: 'YOUR_API_KEY'
      })

      // Send the transaction to the Transaction Service with the signature from Owner A
      await apiKit.proposeTransaction({
        safeAddress: config.SAFE_ADDRESS,
        safeTransactionData: safeTransaction.data,
        safeTxHash,
        senderAddress: config.OWNER_A_ADDRESS,
        senderSignature: signatureOwnerA.data
      })
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```python
      # Instantiate the Transaction Service API
      transaction_service_api = TransactionServiceApi(
          network=EthereumNetwork.SEPOLIA,
          ethereum_client=ethereum_client,
          api_key="YOUR_API_KEY",  # You can also use an environment variable: SAFE_TRANSACTION_SERVICE_API_KEY
      )

      # Send the transaction to the Transaction Service with the signature from Owner A
      transaction_service_api.post_transaction(safe_tx)
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      We skip this step because the transaction we created using Curl is already in the Safe Queue Service. Check the [Create a Safe transaction](#create-a-safe-transaction) step.
    </Tabs.Tab>
  </Tabs>


  ### Collect missing signatures

  #### Get the pending transaction


  <Tabs items={['TypeScript', 'Python', 'Curl']}>
    <Tabs.Tab>
      ```typescript
      const signedTransaction = await apiKit.getTransaction(safeTxHash)
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```python
      (safe_tx_from_tx_service, _) = transaction_service_api.get_safe_transaction(
          safe_tx_hash)
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```bash
      curl -X 'GET' \
      'https://api.safe.global/queue/api/v1/multisig-transactions/0x56b2931ffffffffff303bbaadf3ba29b5c2baafdf1a5ffffffffff62674941b6' \
      -H 'accept: application/json' \
      -H 'Authorization: Bearer YOUR_API_KEY'
      ```
    </Tabs.Tab>
  </Tabs>


  #### Add missing signatures


  <Tabs items={['TypeScript', 'Python', 'Curl']}>
    <Tabs.Tab>
      ```typescript
      // Initialize the Protocol Kit with Owner B
      const protocolKitOwnerB = await Safe.init({
        provider: config.RPC_URL,
        signer: config.OWNER_B_PRIVATE_KEY,
        safeAddress: config.SAFE_ADDRESS
      })

      // Sign the transaction with Owner B
      const signatureOwnerB = await protocolKitOwnerB.signHash(safeTxHash)

      // Send the transaction to the Transaction Service with the signature from Owner B
      await apiKit.confirmTransaction(
        safeTxHash,
        signatureOwnerB.data
      )
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```python
      # Sign the transaction with Owner B
      owner_b_signature = safe_tx_from_tx_service.sign(
          config.get("OWNER_B_PRIVATE_KEY"))

      # Send the transaction to the Transaction Service with the signature from Owner B
      transaction_service_api.post_signatures(
          safe_tx_from_tx_service.safe_tx_hash,
          owner_b_signature)
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```bash
      curl -X 'POST' \
      'https://api.safe.global/queue/api/v1/multisig-transactions/0x56b2931ffffffffff303bbaadf3ba29b5c2baafdf1a5ffffffffff62674941b6/signatures' \
      -H 'accept: application/json' \
      -H 'Content-Type: application/json' \
      -H 'Authorization: Bearer YOUR_API_KEY' \
      -d '{
          "signatures": [
              "0x8c41aaa029db2942a1574c3ec9442b80764e953f21f994edb641d33ffffffffffa55ffffffffffefcaea5b3360bd4c789803fe289b2ad467fc9b3bedb479dad20"
          ]
      }'
      ```
    </Tabs.Tab>
  </Tabs>


  ### Execute the transaction


  <Tabs items={['TypeScript', 'Python']}>
    <Tabs.Tab>
      ```typescript
      const transactionResponse =
        await protocolKitOwnerA.executeTransaction(signedTransaction)
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```python
      result = safe_tx_from_tx_service.execute(config.get("OWNER_A_PRIVATE_KEY"))
      ```
    </Tabs.Tab>
  </Tabs>


  ### Get the executed transaction


  <Tabs items={['TypeScript', 'Python', 'Curl']}>
    <Tabs.Tab>
      ```typescript
      const transactions = await apiKit.getMultisigTransactions(config.SAFE_ADDRESS)

      if (transactions.results.length > 0) {
        console.log('Last executed transaction', transactions.results[0])
      }
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      ```python
      transactions = transaction_service_api.get_transactions(
          config.get("SAFE_ADDRESS"))
      
      last_executed_tx = next(
          (x for x in transactions if x.get('isExecuted')),
          None)
      ```
    </Tabs.Tab>
    <Tabs.Tab>
      The Safe Transaction Service serves the execution details of a transaction, such as its execution date and block number, so this request calls it instead of the Safe Queue Service.

      ```bash
      curl -X 'GET' \
      'https://api.safe.global/tx-service/sep/api/v2/safes/0xc62C5cbB964459F3C984682f78A4d3ffffffffff/multisig-transactions/?executed=true&limit=1' \
      -H 'accept: application/json' \
      -H 'Authorization: Bearer YOUR_API_KEY'
      ```
    </Tabs.Tab>
  </Tabs>

</Steps>
