Skip to content

Latest commit

 

History

History
209 lines (145 loc) · 3.7 KB

File metadata and controls

209 lines (145 loc) · 3.7 KB

API Documentation

HTTP endpoints for Hypermind integration and development.

Endpoints

You can test all the available endpoints running: nude test-api.js.

GET /api/stats

Returns node statistics and swarm information.

{
  "count": 42,
  "totalUnique": 1337,
  "direct": 8,
  "id": "abc123...",
  "screenname": "BraveElephant",
  "diagnostics": {...},
  "chatEnabled": true,
  "mapEnabled": true,
  "peers": [...]
}
POST /api/chat

Send P2P chat message. Rate limited: 5 messages per 5 seconds.

{
  "content": "Hello, world!",
  "scope": "GLOBAL",
  "target": null
}
GET /api/github/latest-release

Latest GitHub release information.

{
  "tag_name": "v1.2.3",
  "html_url": "https://github.com/lklynet/hypermind/releases/tag/v1.2.3",
  "published_at": "2026-01-08T12:00:00Z",
  "body": "Release notes..."
}
GET /events

Server-Sent Events stream for real-time updates. Returns same data as /api/stats.

GET /js/lists.js

Dynamic JavaScript file containing adjectives and nouns for screenname generation.

GET /js/screenname.js

Dynamic JavaScript file with screenname generation logic for browser.

GET /

Main HTML page with server-side template rendering.

Development

Route Structure

Routes are modular in src/web/routes/:

src/web/routes/
├── your-new-route.js

Creating Routes

1. Create module

src/web/routes/your-new-route.js:

const setupYourNewRouteRoutes = (router, dependencies) => {
  const { identity, peerManager } = dependencies;

  router.get("/api/your-new-route", (req, res) => {
    res.json({ success: true });
  });
};

module.exports = { setupYourNewRouteRoutes };
2. Register route

In src/web/routes.js:

const { setupYourNewRouteRoutes } = require("./routes/your-new-route");

const yourNewRouteDeps = { identity, peerManager };
setupYourNewRouteRoutes(app, yourNewRouteDeps);
3. Add constants

In src/config/constants.js:

const YOUR_CONFIG = {
  enabled: process.env.ENABLE_YOUR_FEATURE === "true",
  rateLimit: parseInt(process.env.YOUR_RATE_LIMIT) || 1000,
};

module.exports = { YOUR_CONFIG };

Security

Rate limiting
let requestHistory = [];

router.get("/api/your-new-route", (req, res) => {
  const now = Date.now();
  requestHistory = requestHistory.filter((time) => now - time < 10000);

  if (requestHistory.length >= 5) {
    return res.status(429).json({ error: "Rate limit exceeded" });
  }

  requestHistory.push(now);
});
Input validation
router.post("/api/your-new-route", (req, res) => {
  const { data } = req.body;

  if (!data || typeof data !== "string" || data.length > 1000) {
    return res.status(400).json({ error: "Invalid data" });
  }
});
Error handling
router.get("/api/your-new-route", async (req, res) => {
  try {
    const result = await someOperation();
    res.json({ success: true, data: result });
  } catch (error) {
    console.error("Error:", error);
    res.status(500).json({ error: "Operation failed" });
  }
});

Testing

curl http://localhost:3000/api/your-new-route