# Magic Signer

In this guide, you will learn how to create a [Magic](https://magic.link/) signer that can be added as a Safe owner and used to initialize any of the kits from the Safe\{Core\} SDK.

Check out the [Safe Signers demo app](https://github.com/5afe/safe-signers) on GitHub to follow along this guide with the completed code.

## Prerequisites

- [Node.js and npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm).
- A [Magic account](https://dashboard.magic.link) and an API key.

## Install dependencies


<Tabs items={['npm', 'yarn', 'pnpm']}>
  <Tabs.Tab>
    ```bash
    npm install magic-sdk
    ```
  </Tabs.Tab>
  <Tabs.Tab>
    ```bash
    yarn add magic-sdk
    ```
  </Tabs.Tab>
  <Tabs.Tab>
    ```bash
    pnpm add magic-sdk
    ```
  </Tabs.Tab>
</Tabs>


## Steps

<Steps>

  ### Imports

  Here are the necessary imports for this guide.


  ```typescript
  import { Magic } from 'magic-sdk'
  ```


  In addition, you will need to import a web3 library of your choice to use in the "Get the provider and signer" section. In this guide, we are using `viem`.

  ### Get the API Key

  Check Magic documentation on how to create a new project in their [dashboard](https://dashboard.magic.link) and [get the API key.](https://magic.link/docs/home/quickstart/cli#add-your-api-key)

  Once you have it, you need to initialize the `MAGIC_API_KEY` variable with it.


  ```typescript
  const MAGIC_API_KEY = // ...
  ```


  ### Initialize Magic

  Magic initialization is done by instantiating the `Magic` class with an `MAGIC_API_KEY`. Optionally, it also accepts some configuration parameters that can be checked in the [Magic Web API Reference](https://magic.link/docs/api/client-side-sdks/web).


  ```typescript
  const magic = new Magic(MAGIC_API_KEY)
  ```


  ### Login

  To login with an email address or social account you need to call the following method, that will open the popup and request the user to submit the login form.


  ```typescript
  await magic.wallet.connectWithUI()
  ```


  ### Get the provider and signer

  Once the user is logged in, you can get the `provider` and `signer`, which is the externally-owned account of the user that was derived from its credentials.


  <Tabs items={['viem']}>
    <Tabs.Tab>
      You can instantiate the provider using `viem` and the following imports:

      ```typescript
      import { createWalletClient, custom } from 'viem'
      import { sepolia } from 'viem/chains'
      ```

      ```typescript
      const provider = createWalletClient({
        chain: sepolia,
        transport: custom(magic.rpcProvider)
      })

      const metadata = await magic.user.getInfo()
      const signer = metadata.publicAddress
      ```
    </Tabs.Tab>
  </Tabs>


  With the `provider` and `signer` you are ready to instantiate any of the kits from the Safe\{Core\} SDK and set up or use this signer as a Safe owner.


  ### Logout

  Finally, to logout the user, call the `logout()` method.


  ```typescript
  await magic.user.logout()
  ```


</Steps>

## Recap and further reading

After following this guide, you are able to create a Safe signer using Magic and get the `provider` and `signer` required to initialize the kits from the Safe\{Core\} SDK.

Learn more about Magic by checking the following resources:

- [Magic website](https://magic.link)
- [Magic documentation](https://magic.link/docs)
- [Magic quickstart guide](https://magic.link/docs/home/quickstart/cli)
