# Safe Deployment

This guide will teach you how to deploy a new Safe using the Protocol Kit. This process includes initializing the Protocol Kit, setting up your Safe configuration, and executing the deployment.

For more detailed information, see the [Protocol Kit Reference](../../../reference-sdk-protocol-kit/overview.mdx).

## Prerequisites

- [Node.js and npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm)

## Install dependencies

First, you need to install some dependencies.


```bash
pnpm add @safe-global/protocol-kit viem
```


## Steps

<Steps>

  ### Imports

  Here are all the necessary imports for this guide.


  ```typescript
  import Safe, {
    PredictedSafeProps,
    SafeAccountConfig,
    SafeDeploymentConfig
  } from '@safe-global/protocol-kit'
  import { sepolia } from 'viem/chains'
  ```


  ### Create a signer

  You need a signer to instantiate the Protocol Kit. This example uses a private key to obtain a signer, but [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) compatible signers are also supported. For detailed information about signers, please refer to the [Protocol Kit reference](../../../reference-sdk-protocol-kit/overview.mdx).


  ```typescript
  const SIGNER_PRIVATE_KEY = // ...
  ```


  ### Initialize the Protocol Kit
  
  Initialize an instance of the Protocol Kit for each network where you want to deploy a new Safe smart account by calling the [`init`](../../../reference-sdk-protocol-kit/initialization/init.mdx) method. Pass the `provider` with its corresponding value depending on the network, the `signer` executing the deployment, and the [`predictedSafe`](../../../reference-sdk-protocol-kit/initialization/init.mdx#predictedsafe-optional) with the Safe account configuration.

  Optionally, you can [track your Safe deployments and transactions on-chain](../../onchain-tracking.mdx) by using the `onchainAnalytics` property.


  ```typescript
  const safeAccountConfig: SafeAccountConfig = {
    owners: ['0x...', '0x...', '0x...'],
    threshold: 2
    // More optional properties
  }

  const predictedSafe: PredictedSafeProps = {
    safeAccountConfig
    // More optional properties
  }

  const protocolKit = await Safe.init({
    provider: sepolia.rpcUrls.default.http[0],
    signer: SIGNER_PRIVATE_KEY,
    predictedSafe,
    onchainAnalytics // Optional
  })
  ```


  ### Predict the Safe address

  You can predict the Safe address using the [`getAddress`](../../../reference-sdk-protocol-kit/safe-info/getaddress.mdx) method in the Protocol Kit.


  ```typescript
  const safeAddress = await protocolKit.getAddress()
  ```


  ### Create the deployment transaction

  Create the deployment transaction to deploy a new Safe smart account by calling the [`createSafeDeploymentTransaction`](../../../reference-sdk-protocol-kit/deployment/createsafedeploymenttransaction.mdx) method.


  ```typescript
  const deploymentTransaction = await protocolKit.createSafeDeploymentTransaction()
  ```


  ### Execute the deployment transaction

  Once the deployment transaction object is ready, execute it using the provided signer or your preferred external Ethereum client.


  ```typescript
  const client = await protocolKit.getSafeProvider().getExternalSigner()

  const transactionHash = await client.sendTransaction({
    to: deploymentTransaction.to,
    value: BigInt(deploymentTransaction.value),
    data: deploymentTransaction.data as `0x${string}`,
    chain: sepolia
  })

  const transactionReceipt = await client.waitForTransactionReceipt({
    hash: transactionHash
  })
  ```


  ### Reinitialize the Protocol Kit

  Once the deployment transaction is executed, connect the new Safe address to the Protocol Kit instance by calling the [`connect`](../../../reference-sdk-protocol-kit/initialization/connect.mdx) method.


  ```typescript
  const newProtocolKit = await protocolKit.connect({
    safeAddress
  })

  const isSafeDeployed = await newProtocolKit.isSafeDeployed() // True
  const safeAddress = await newProtocolKit.getAddress()
  const safeOwners = await newProtocolKit.getOwners()
  const safeThreshold = await newProtocolKit.getThreshold()
  ```


</Steps>

## Recap and further reading

After following this guide, you are able to deploy new Safe smart accounts with the Protocol Kit.
