> For the complete documentation index, see [llms.txt](https://docs.kanalabs.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kanalabs.io/~/changes/JpxQ3y4p9AD4BHUqimHx/products-and-features/trading-apis/kana-perps-api/example-setup-functions.md).

# Example setup functions

## We will see how to implement the run functions : (Example deposit function)

1\. Imports:

```typescript
import { AptosConfig, Aptos, Network, Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk";
import axios from "axios";

async function main(): Promise<void> {
```

\
2\. Aptos client setup:

```typescript
    // Setup the Aptos client configuration
    const config = new AptosConfig({ network: Network.TESTNET }); // Connect to Aptos Testnet
    const aptos = new Aptos(config); // Create an instance of the Aptos SDK
```

\
3\. Wallet setup:

<pre class="language-typescript"><code class="lang-typescript">    // Initialize the account using a private key
    const formattedPrivateKey = PrivateKey.formatPrivateKey(
    process.env.APTOS_PRIVATEKEY || '',
    'ed25519' as PrivateKeyVariants
    );
<strong>    const account = Account.fromPrivateKey({
</strong>    privateKey: new Ed25519PrivateKey(formattedPrivateKey),
    });
</code></pre>

4\. Get transaction payload: (required function parameters for order deposit function)

```typescript
    // API endpoint for fetching limit order payload
    const baseURL = 'https://perps-tradeapi.kanalabs.io/limitOrder';
    const params = {
        marketId: 66, // Specify the market ID for the trade
        amount: 50000, // amount to deposit your trading account
    };
    // Fetch the payload for deposit
    const res = await axios.get(baseURL, {
        params, 
        headers: {
            'x-api-key': process.env.API_KEY,
        },
    });
    const payload = res.data; // Extract the data payload
```

\
5\. Building transaction

```typescript
    // Build a transaction payload using the Aptos SDK
    const transactionPayload = await aptos.transaction.build.simple({
        sender: account.accountAddress, // The address of the account sending the transaction
        data: payload, // Payload fetched from the API
    });
```

\
6\. Sign and Submit Transaction

```typescript
    // Sign and submit the transaction
    const committedTxn = await aptos.transaction.signAndSubmitTransaction({
        transaction: transactionPayload, // Built transaction payload
        signer: account, // Account used for signing
    });
```

\
7\. Wait for transaction to complete

```typescript
    // Wait for the transaction to be confirmed and retrieve its status
    const response = await aptos.waitForTransaction({
        transactionHash: committedTxn.hash, // Hash of the submitted transaction
    });
    // Log the success status of the transaction
    console.log("response", response.success);
```

8\. Call main function:

```typescript
}
// Run the main function and handle any errors
main().catch(error => {
    console.error('An error occurred:', error);
});
```

## We will see how to implement the view functions : (Example get market info function)

#### 1. **Imports:**

```typescript
// Import the axios library for making HTTP requests
import axios from "axios";
```

#### 2. **Main Function Setup:**

```typescript
// Main function to fetch market info from the API
async function main(): Promise<void> {
```

#### 3. **API Endpoint Setup:**

```typescript
// The base URL for the API endpoint
const baseURL = 'https://perps-tradeapi.kanalabs.io/getMarketInfo';
```

#### 4. **Parameters Setup:**

```typescript
// Parameters to be sent with the API request (marketId in this case)
const params = {
      marketId: 'your_market_id' // Replace with the actual marketId
  };
```

#### 5. **API Request:**

```typescript
try {
    // Sending a GET request to the API with the parameters and the API key
    const res = await axios.get(baseURL, {
        params, // Query parameters to be included in the request URL
        headers: {
            'x-api-key': process.env.API_KEY, // API key passed in the headers from environment variables
        },
    });
```

#### 6. **Extracting and Logging Data:**

```typescript
// Extracting the data returned by the API response
const getMarketInfo = res;
// Logging the market info data to the console
console.log("getMarketInfo: ", getMarketInfo);
```

#### 7. **Error Handling:**

```typescript
} catch (error) {
        // Handling errors if the API request fails
        console.error('An error occurred:', error);
    }
```

#### 8. **Calling the Main Function:**

```typescript
// Calling the main function and handling potential errors
main().catch(error => {
    console.error('An error occurred:', error);
});
```
