# SDK Integration

This guide helps developers integrate Clique Wallet into their applications using the official SDK. The Clique Wallet SDK is available as an npm package and provides a convenient way to interact with Clique Wallet's API.

#### Prerequisites

* Access to a Clique Wallet instance
* The Clique Wallet API endpoint URL
* Node.js 16+ or a modern JavaScript environment

#### Installation

Install the Clique Wallet SDK using npm:

```bash
npm install clique-wallet-sdk
```

Or using yarn:

```bash
yarn add clique-wallet-sdk
```

For more information, visit the [Clique Wallet SDK on npm](https://www.npmjs.com/package/clique-wallet-sdk).

#### Basic Usage

**Initialize the SDK**

```javascript
import { CliqueWallet } from 'clique-wallet-sdk';

const wallet = new CliqueWallet({
  apiUrl: 'https://your-wallet-instance.com'
});
```

**Authenticate User**

```javascript
// Request verification code
await wallet.sendVerification({ email: 'user@example.com' });

// Complete login with verification code
const user = await wallet.login({
  type: 'Email',
  data: {
    email: 'user@example.com',
    code: '123456'
  }
});
```

**Get Wallet Addresses**

```javascript
// Get current session and wallet information
const session = await wallet.getSession();

// Access wallet addresses
const ethereumWallet = session.wallets.find(w => w.network === 'Ethereum');
const solanaWallet = session.wallets.find(w => w.network === 'Solana');
```

**Sign Transaction**

```javascript
const signature = await wallet.sign({
  address: '0x...',
  network: 'Ethereum',
  message: '0x...',
  need_check: true
});
```

#### Authentication Methods

The SDK supports all authentication methods available in Clique Wallet:

**Email/Phone Verification:**

```javascript
await wallet.sendVerification({ email: 'user@example.com' });
// or
await wallet.sendVerification({ phone: '+1234567890' });
```

**Social OAuth:**

```javascript
// Initialize OAuth flow
const oauthUrl = await wallet.oauth.init({
  provider: 'google',
  redirectUri: 'https://yourapp.com/callback'
});

// After OAuth callback, complete login
await wallet.login({
  type: 'GoogleOAuth',
  data: { state, code, code_verifier }
});
```

**External Wallet Connection:**

```javascript
// Get challenge
const challenge = await wallet.getChallenge({
  method: 'metamask',
  id: '0x...'
});

// After user signs, complete login
await wallet.login({
  type: 'MetaMask',
  data: { wallet_address, challenge, signature }
});
```

#### Error Handling

The SDK provides structured error handling:

```javascript
try {
  const user = await wallet.login({ /* ... */ });
} catch (error) {
  if (error.status === 401) {
    // Handle authentication error
  } else if (error.status === 400) {
    // Handle bad request
  } else {
    // Handle other errors
  }
}
```
