> For the complete documentation index, see [llms.txt](https://axions-organization.gitbook.io/axion-framework/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://axions-organization.gitbook.io/axion-framework/api-documentation-for-axion-framework.md).

# API Documentation for Axion Framework

####

The **Axion Framework** provides robust APIs for developers to interact with swarm nodes, integrate LLMs, manage tasks, and enable decentralized collaboration efficiently.

***

#### 1. **SwarmNode Class**

The `SwarmNode` class facilitates decentralized communication between agents using IPFS.

**Methods**

* **`send_decentralized_message(message)`**\
  Send a message via IPFS.

  **Example:**

  ```python
  node.send_decentralized_message("Hello, world!")
  ```
* **`retrieve_decentralized_message(ipfs_hash)`**\
  Retrieve a message from IPFS using its hash.

  **Example:**

  ```python
  node.retrieve_decentralized_message("QmHashHere")
  ```

***

#### 2. **TaskScheduler Class**

The `TaskScheduler` class manages task allocation and distribution among agents in the swarm.

**Methods**

* **`add_task(node_id, task, priority)`**\
  Add a task to the scheduler with an assigned priority.

  **Example:**

  ```python
  scheduler.add_task(1, "Process data", priority=5)
  ```
* **`assign_task(nodes)`**\
  Dynamically assign tasks to nodes based on priority.

  **Example:**

  ```python
  scheduler.assign_task(swarm.nodes)
  ```

***

#### 3. **SwarmConsensus Class**

The `SwarmConsensus` class handles collaborative decision-making within the swarm.

**Methods**

* **`propose_task(task_description)`**\
  Propose a task for the swarm to vote on.

  **Example:**

  ```python
  proposal_id = swarm.propose_task("Analyze data trends")
  ```
* **`vote(proposal_id)`**\
  Vote on a task proposal.

  **Example:**

  ```python
  swarm.vote(proposal_id)
  ```
* **`get_consensus()`**\
  Check if a task proposal has reached consensus.

  **Example:**

  ```python
  consensus = swarm.get_consensus()
  print(consensus)
  ```

***

#### 4. **IPFSClient Class**

The `IPFSClient` class allows agents to store and retrieve data in a decentralized manner using IPFS.

**Methods**

* **`upload_file(file_path)`**\
  Upload a file to IPFS.

  **Example:**

  ```python
  cid = ipfs_client.upload_file("data/task_data.json")
  print(f"Uploaded to IPFS with CID: {cid}")
  ```
* **`retrieve_file(cid, output_path)`**\
  Retrieve a file from IPFS using its CID.

  **Example:**

  ```python
  ipfs_client.retrieve_file(cid, output_path="downloaded_data.json")
  ```

***

#### 5. **Task Execution Examples**

**Example 1: Running a Swarm Simulation**

Simulate a swarm of 10 nodes for 5 iterations:

```python
from src.swarm.advanced_swarm_behavior import Swarm

swarm = Swarm(10)
swarm.simulate(5)
```

**Example 2: Decentralized Messaging**

Send and retrieve messages using IPFS:

```python
# Send a message
node.send_decentralized_message("Task completed successfully.")

# Retrieve a message
message = node.retrieve_decentralized_message("QmHashHere")
print(f"Retrieved message: {message}")
```

**Example 3: Task Scheduling**

Add tasks to the scheduler and dynamically assign them to agents:

```python
scheduler.add_task(1, "Optimize reinforcement learning parameters", priority=5)
scheduler.assign_task(swarm.nodes)
```

***

#### 6. **Integration Notes**

**Environment Variables**

Securely configure wallets for blockchain integration:

```bash
export SOLANA_WALLET_PATH=/path/to/solana-wallet.json
export ETHEREUM_WALLET_PRIVATE_KEY=your_private_key_here
```

**Modular Components**

Each component (e.g., `IPFSClient`, `SwarmConsensus`) operates independently or as part of an integrated workflow.

**Extensibility**

Developers can extend the core classes to implement custom workflows or adapt the framework for specific use cases.

***

#### 7. **Common Errors and Troubleshooting**

| **Error**                       | **Solution**                                                               |
| ------------------------------- | -------------------------------------------------------------------------- |
| FileNotFoundError: Wallet path  | Ensure the `SOLANA_WALLET_PATH` environment variable is set correctly.     |
| ValueError: Private key missing | Set the `ETHEREUM_WALLET_PRIVATE_KEY` variable in your environment.        |
| Consensus not reached           | Verify active voting agents or increase the consensus threshold as needed. |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://axions-organization.gitbook.io/axion-framework/api-documentation-for-axion-framework.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
