API v1
Purpose
Describes the REST endpoints in API v1 used to interact with HieraChain: chain management, event recording, proof submission, entity tracing, statistics, and block retrieval.
Appendix: Event Submission with IPFS Example
When enterprises need to store large files (PDF contracts, product images) without bloating the blockchain:
- Step 1: Upload the file to IPFS and get a
CID. - Step 2: Submit the event to HieraChain with
details_cid.
curl -X POST "http://localhost:2661/api/v1/chains/supply_chain/events" \
-H "Content-Type: application/json" \
-H "X-API-Key: your_api_key" \
-d '{
"entity_id": "CONTRACT-2024-001",
"event": "contract_signed",
"details_cid": "QmXoypizjW3WknFiJnKLwHCnL72vedxjQkDDP1mXWo6uco",
"details_nonce": "98765"
}'
This data will be integrity-guaranteed by HieraChain through hash chaining, while the actual content is securely stored on the IPFS network.
Endpoint Overview
sequenceDiagram
participant Client
participant API as API v1
participant Sub as Sub-Chain
participant Main as Main Chain
Client->>API: POST /chains/create
API->>Sub: Initialize Sub-Chain
API-->>Client: 201 Created
Client->>API: POST /chains/events
API->>Sub: Add Event
Sub->>Sub: Close Block
API-->>Client: 200 OK (Event ID)
Client->>API: POST /chains/submit-proof
API->>Sub: Get Proof
Sub->>Main: Submit Proof (Data Anchoring)
Main-->>Sub: Confirm
API-->>Client: 200 OK (Proof ID)
- GET
/api/v1/health— Health check. - GET
/api/v1/chains— List Main Chain and all Sub-Chains. - POST
/api/v1/chains/{chain_name}/create— Create a new Sub-Chain (auto-creates Main Chain if not exists). - POST
/api/v1/chains/{chain_name}/events— Add an event to a Sub-Chain. - POST
/api/v1/chains/{chain_name}/submit-proof— Submit proof from Sub-Chain to Main Chain. - GET
/api/v1/chains/{chain_name}/stats— Get chain statistics. - GET
/api/v1/chains/{chain_name}/blocks?limit=10&offset=0&resolve_cid=false— Get block list (paginated). Ifresolve_cid=true, automatically load detailed data from IPFS. - GET
/api/v1/chains/{chain_name}/blocks/{index_or_hash}— Get details of a specific block. - GET
/api/v1/entities/{entity_id}/trace[?chain_name=...&resolve_cid=false]— Trace events. Ifresolve_cid=true, decrypt event details from IPFS.
Main Schemas (from hierachain/api/v1/schemas.py)
-
EventRequestentity_id: strevent_type: strdetails: dict[str, Any] | None(On-chain data)details_cid: str | None(Off-chain CID reference)details_nonce: str | None(Encryption nonce)details_metadata: dict[str, Any] | None
-
EventResponsesuccess: boolmessage: strevent_id: str | None
-
ChainInfoResponsename: strtype: str("main" | "sub")block_count: intlatest_block_hash: str | None
-
ProofSubmissionResponsesuccess: boolmessage: strproof_id: str | None
-
EntityTraceResponseentity_id: strchains: list[str]events: list[dict[str, Any]]
-
ChainStatsResponsechain_name: strtotal_blocks: inttotal_events: intproof_count: int | Noneregistered_sub_chains: int | None
Usage Examples
Assuming the server is running at http://localhost:2661:
1. Health check
2. Create Sub-Chain
Response (201):
{
"success": true,
"message": "Sub-chain 'supply_chain' created successfully",
"chain_name": "supply_chain"
}
3. Add event to Sub-Chain
curl -X POST http://localhost:2661/api/v1/chains/supply_chain/events \
-H "Content-Type: application/json" \
-d '{
"entity_id": "PROD-001",
"event_type": "production_complete",
"details": {"quantity": 100}
}'
Response:
{
"success": true,
"message": "Event added to chain 'supply_chain'",
"event_id": "supply_chain_1_1"
}
4. Submit proof to Main Chain
Response:
{
"success": true,
"message": "Proof submitted from 'supply_chain' to main chain",
"proof_id": "supply_chain_1"
}
5. Get block details
Endpoint: GET /api/v1/chains/{chain_name}/blocks/{index_or_hash}
Get detailed data of a specific block by Index (number) or Hash (string).
Query Parameters:
resolve_cid(boolean): IfTrue, the server will decryptdetails_cidfrom IPFS and return the original data indetailsfield.
Example:
curl -X GET "http://localhost:2661/api/v1/chains/supply_chain/blocks/10?resolve_cid=true" \
-H "X-API-Key: your_api_key"
6. Trace entity across all chains
Or limited to a single chain:
6. Get chain statistics
7. Get paginated blocks
Status Codes & Common Errors
- 200 OK — Success for most GET/POST cases.
- 201 Created — Sub-Chain created successfully.
- 400 Bad Request — Invalid
chain_namewhen creating (only allows[a-zA-Z0-9_\-]). - 404 Not Found — Chain or sub-chain not found.
- 500 Internal Server Error — Internal processing error (e.g., error when listing chains, adding events, submitting proofs, statistics, or retrieving blocks).
Implementation Notes (abbreviated from endpoints.py)
- Lazy DI: uses lightweight singletons
get_hierarchy_manager()andget_entity_tracer()for request lifecycle. POST /chains/{chain_name}/events: server setstimestamp = time.time(); missingdetailsdefaults to{}.POST /chains/{chain_name}/submit-proof: ifSubChainhas nosubmit_proof_to_main, the endpoint falls back to a mock branch to avoid crash.GET /chains/{chain_name}/blocks: whenBlockhas noto_event_list, there is a fallback conversion from Arrow Table (to_pylist) for safety.
Related
- Overall architecture: Overview
- Hierarchical module: Hierarchical
- Core module: Core