> ## Documentation Index
> Fetch the complete documentation index at: https://docs.compasslabs.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Safe

> Learn how to use Compass API SDK with Safe for smart account operations. 

## Using Compass API with Safe Smart Accounts

This guide demonstrates how to integrate Compass API SDK with Safe Starter Kit SDK to create and use smart accounts for DeFi operations.

## Prerequisites

<Steps>
  <Step title="Install Dependencies">
    Install the required packages:

    ```bash theme={"system"}
    npm install @safe-global/sdk-starter-kit @compass-labs/api-sdk dotenv 
    ```
  </Step>

  <Step title="Set Environment Variables">
    Create a `.env` file in your project root:

    ```bash theme={"system"}
    ARBITRUM_RPC_URL="your_arbitrum_rpc_url"
    SIGNER_PRIVATE_KEY="your_wallet_private_key"
    COMPASS_API_KEY="your_compass_api_key" 
    # If using existing safe:
    SAFE_ADDRESS="your_safe_address" 
    # If creating new safe: #
    SIGNER_ADDRESS="your_signer_address"
    ```
  </Step>
</Steps>

## Implementation Guide

<Steps>
  <Step title="Import dependencies and load environment">
    First, import the necessary dependencies and load your environment variables.

    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v0/smart_accounts/safe/src/index.ts",
snippetNumber: 1,
}}
    />
  </Step>

  <Step title="Create Safe client and initialize Compass API SDK">
    You have two options for setting up your Safe client:

    <ul>
      <li>
        Option A:

        Use an existing Safe by providing its address.
      </li>

      <li>
        Option B:

        Deploy a new Safe by providing the signer's
        address (uncomment the relevant code in the script).
      </li>
    </ul>

    This step also initializes the Compass API SDK for DeFi operations.

    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v0/smart_accounts/safe/src/index.ts",
snippetNumber: 2,
}}
    />
  </Step>

  <Step title="Create and prepare batched operations">
    Here's an example of increasing allowance and supplying tokens to Aave. If you
    are deploying a new Safe, only include the `ALLOWANCE_INCREASE` operation in
    the first transaction. The Safe will not have any tokens initially, so the
    `AAVE_SUPPLY` operation should be executed in a subsequent transaction after
    funding the Safe with USDC.

    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v0/smart_accounts/safe/src/index.ts",
snippetNumber: 3,
}}
    />
  </Step>

  <Step title="Send batched operations and handle result">
    Convert the operations to the format expected by Safe and execute them. The script will print the transaction result or an error if the wallet is not funded.

    <GithubCodeBlock
      typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v0/smart_accounts/safe/src/index.ts",
snippetNumber: 4,
}}
    />
  </Step>
</Steps>

## Full Code

Here is the full script from the tutorial. Copy and paste in your code editor and play around!

<GithubCodeBlock
  typescript={{
name: "index.ts",
url: "https://raw.githubusercontent.com/CompassLabs/api_usecases/main/v0/smart_accounts/safe/src/index.ts",
}}
  numOfLinesExpandable={95}
/>

## Common Operation Examples

Here are some examples of different DeFi operations you can perform:

### Token Swaps

```typescript theme={"system"}
{
  body: {
    actionType: "UNISWAP_EXACT_IN",
    tokenIn: "USDC",
    tokenOut: "WETH",
    amountIn: "100",
    amountOutMinimum: "0.05"
  }
}
```

### Lending Operations

```typescript theme={"system"}
{
  body: {
    actionType: "AAVE_SUPPLY",
    token: "USDC",
    amount: "1000"
  }
}
```

### Token Approvals

```typescript theme={"system"}
{
  body: {
    actionType: "ALLOWANCE_INCREASE",
    token: "WETH",
    contractName: "UniswapV3Router",
    amount: "1"
  }
}
```

## Resources

<CardGroup cols={2}>
  <Card title="Compass API Docs" icon="compass" href="https://docs.compasslabs.ai/v1/api-reference/smart-account/get-smart-account-batched-user-operations">
    Access detailed API documentation and references
  </Card>

  <Card title="Safe Starter Kit Documentation" icon="shield" href="https://docs.safe.global/sdk/starter-kit">
    Learn more about Safe's smart account features
  </Card>

  <Card title="GitHub Examples" icon="github" href="https://github.com/CompassLabs/api_usecases/tree/main/v0/smart_accounts/safe">
    View more code examples and implementations
  </Card>
</CardGroup>
