# API and SDK

#### **API and SDK Documentation**

The XSPACE Protocol provides a robust suite of tools, including **REST APIs**, **Web3 integration**, and the **XSPACE SDK**, to empower developers in building decentralized applications and interacting with the blockchain. These resources simplify operations such as querying the blockchain, managing tokens, and interacting with smart contracts.

***

### **REST APIs**

XSPACE’s REST APIs allow developers to interact with the blockchain without running a full node. They are ideal for querying data, managing accounts, and executing basic blockchain operations.

#### **Base URL**

* **Testnet**: `https://api.testnet.xspaceprotocol.io`
* **Mainnet**: `https://api.xspaceprotocol.io`

***

#### **API Endpoints**

**1. Account Management**

* **Get Account Balance**
  * **Endpoint**: `GET /account/{address}/balance`
  * **Description**: Retrieve the balance of an account.
  * **Parameters**:
    * `address` (string): The wallet address.
  * **Response**:

    ```json
    {
        "address": "0x123456789abcdef",
        "balance": "1000000000000000000" // Balance in wei
    }
    ```
* **List Transactions**
  * **Endpoint**: `GET /account/{address}/transactions`
  * **Description**: Fetch a list of transactions for a specific account.
  * **Parameters**:
    * `address` (string): The wallet address.
  * **Response**:

    ```json
    [
        {
            "txHash": "0xabcdef123456789",
            "from": "0x123456789abcdef",
            "to": "0xabcdef987654321",
            "value": "500000000000000000",
            "timestamp": "1672444800"
        }
    ]
    ```

***

**2. Smart Contracts**

* **Call Smart Contract Function**
  * **Endpoint**: `POST /contract/{address}/call`
  * **Description**: Call a read-only function on a smart contract.
  * **Parameters**:
    * `address` (string): The contract address.
    * `method` (string): The method signature (e.g., `getBalance()`).
    * `params` (array): Parameters for the method.
  * **Response**:

    ```json
    {
        "result": "1000000000000000000"
    }
    ```
* **Submit Contract Transaction**
  * **Endpoint**: `POST /contract/{address}/transaction`
  * **Description**: Submit a transaction to invoke a smart contract method.
  * **Parameters**:
    * `address` (string): The contract address.
    * `method` (string): The method signature (e.g., `transfer(address,uint256)`).
    * `params` (array): Parameters for the method.
    * `privateKey` (string): The private key to sign the transaction.
  * **Response**:

    ```json
    {
        "txHash": "0xabcdef123456789"
    }
    ```

***

**3. Token Management**

* **Get Token Details**
  * **Endpoint**: `GET /token/{address}`
  * **Description**: Fetch details about a specific token.
  * **Parameters**:
    * `address` (string): The token contract address.
  * **Response**:

    ```json
    {
        "name": "Galaxy Coin",
        "symbol": "GLXYC",
        "totalSupply": "1000000000000000000000",
        "decimals": 18
    }
    ```

***

#### **Authentication**

Some endpoints (e.g., submitting transactions) require authentication using your wallet’s private key. Always ensure keys are stored securely and never exposed publicly.

***

### **Web3 Integration**

Developers can use **Web3.js** or **Ethers.js** to interact with XSPACE smart contracts, tokens, and the blockchain programmatically.

#### **Setting Up Web3.js**

1. **Install Web3.js**:

   ```bash
   npm install web3
   ```
2. **Connect to the XSPACE Testnet**:

   ```javascript
   const Web3 = require('web3');
   const web3 = new Web3('https://testnet.xspaceprotocol.io/rpc');
   ```
3. **Interact with an Account**:

   ```javascript
   async function getBalance(address) {
       const balance = await web3.eth.getBalance(address);
       console.log("Balance in wei:", balance);
   }

   getBalance("0x123456789abcdef");
   ```
4. **Send a Transaction**:

   ```javascript
   async function sendTransaction(from, to, value, privateKey) {
       const tx = {
           from,
           to,
           value: web3.utils.toWei(value, 'ether'),
           gas: 21000,
           chainId: 80001 // XSPACE Testnet Chain ID
       };

       const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
       const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
       console.log("Transaction receipt:", receipt);
   }

   sendTransaction("0x123...", "0xabc...", "0.1", "YOUR_PRIVATE_KEY");
   ```

***

#### **Using Ethers.js**

1. **Install Ethers.js**:

   ```bash
   npm install ethers
   ```
2. **Connect to the XSPACE Testnet**:

   ```javascript
   const { ethers } = require("ethers");
   const provider = new ethers.providers.JsonRpcProvider("https://testnet.xspaceprotocol.io/rpc");
   ```
3. **Interact with a Smart Contract**:

   ```javascript
   const abi = [
       "function getBalance(address user) public view returns (uint256)",
       "function transfer(address to, uint256 amount) public"
   ];

   const contract = new ethers.Contract("CONTRACT_ADDRESS", abi, provider);

   async function callFunction() {
       const balance = await contract.getBalance("0x123...");
       console.log("User balance:", balance.toString());
   }

   callFunction();
   ```

***

### **XSPACE SDK**

The **XSPACE SDK** simplifies blockchain development by providing prebuilt methods for common operations like querying data, interacting with smart contracts, and managing accounts.

#### **Installing the SDK**

Install the XSPACE SDK via npm:

```bash
npm install @xspace/sdk
```

***

#### **Using the SDK**

**Initialize the SDK**

```javascript
const XSPACE = require('@xspace/sdk');

const sdk = new XSPACE({
    network: 'testnet', // Use 'mainnet' for production
    apiKey: 'YOUR_API_KEY' // Optional if private key management is enabled
});
```

***

**Query Account Balance**

```javascript
async function getAccountBalance(address) {
    const balance = await sdk.account.getBalance(address);
    console.log("Balance:", balance);
}

getAccountBalance("0x123456789abcdef");
```

***

**Deploy a Smart Contract**

```javascript
async function deployContract(bytecode, abi, params) {
    const contract = await sdk.contract.deploy({
        bytecode,
        abi,
        params
    });
    console.log("Contract deployed at:", contract.address);
}
```

***

**Interact with a Contract**

```javascript
async function callContractFunction(contractAddress, abi, method, params) {
    const contract = sdk.contract.at(contractAddress, abi);
    const result = await contract.call(method, params);
    console.log("Result:", result);
}
```

***

#### **Developer Resources**

* **API Documentation**: [https://docs.xspaceprotocol.io](https://docs.xspaceprotocol.io/)
* **SDK Documentation**: [https://sdk.xspaceprotocol.io](https://sdk.xspaceprotocol.io/)
* **Network Status**: [https://status.xspaceprotocol.io](https://status.xspaceprotocol.io/)

This suite of APIs and tools provides developers with everything needed to efficiently build and deploy applications on the XSPACE Protocol.
