This document specifies the API for the CKB Data Service. The service is designed to be a generic, decoupled backend, providing factual blockchain data and events. It is not tied to any specific front-end visualization metaphor.
- State vs. Events: The HTTP API provides the current state of any resource. The WebSocket API pushes real-time state change events.
- Standardized Naming: API endpoints and data fields use common blockchain terminology (e.g.,
block,transaction,mempool). - Atomicity: Each event describes a minimal, self-contained state change.
- Discoverability: The API allows drilling down from high-level chain status to detailed transaction information.
The WebSocket API provides real-time, incremental updates about the blockchain. It is ideal for live-updating user interfaces and monitoring tools.
- Endpoint:
ws://localhost:3000/(orwss://for production) - Protocol: Socket.IO protocol with JSON messages.
Clients manage subscriptions by sending messages to the server using Socket.IO events.
-
Client -> Server Message:
// Emit on 'message' event { "action": "subscribe" | "unsubscribe", "channel": "chain" | "transactions" }
-
Server -> Client Messages:
-
Subscription confirmations:
// Received on 'subscribed' event { "channel": "chain" | "transactions" } // Received on 'unsubscribed' event { "channel": "chain" | "transactions" }
-
Data events:
// Received on 'message' event { "channel": "chain" | "transactions", "type": "event.name", "payload": { ... } }
-
Error messages:
// Received on 'error' event { "message": "Error description" }
-
Provides macroscopic events about the blockchain's overall state.
- Event:
block.finalized- Description: Fired when a new block is confirmed as part of the canonical chain. The payload includes summaries of all transactions confirmed in this block, making it ideal for real-time updates without requiring follow-up API calls.
- Payload:
{ "blockNumber": "12345678", "blockHash": "0x...ffaa", "timestamp": "2023-10-27T10:00:00.000Z", "miner": "ckb...xxxx", "reward": "123456789", "transactionCount": 50, "proposalsCount": 120, "unclesCount": 1, "transactions": [ { "txHash": "0x...txhash1" } ] }
Provides fine-grained events about the lifecycle of individual transactions.
-
Event:
transaction.pending- Description: Fired when a transaction is first seen and enters the mempool (
pendingstate). This marks the beginning of its lifecycle. - Payload:
{ "txHash": "0x...abcd", "timestamp": "2023-10-27T10:00:05.123Z", "fee": "10000", "size": "512", "cycles": "3000000" }
- Description: Fired when a transaction is first seen and enters the mempool (
-
Event:
transaction.proposed- Description: Fired when a transaction is included in a block's
proposalsset. - Payload:
{ "txHash": "0x...abcd", "timestamp": "2023-10-27T10:00:10.456Z", "context": { "blockNumber": "12345678", "blockHash": "0x...ffaa" } }
- Description: Fired when a transaction is included in a block's
-
Event:
transaction.confirmed- Description: Fired when a transaction is confirmed on-chain (i.e., its containing block is finalized). This marks the successful end of its lifecycle.
- Payload:
{ "txHash": "0x...abcd", "timestamp": "2023-10-27T10:00:18.789Z", "context": { "blockNumber": "12345678", "blockHash": "0x...ffaa", "txIndexInBlock": 12 } }
-
Event:
transaction.rejected- Description: Fired when a transaction is explicitly rejected by the mempool.
- Payload:
{ "txHash": "0x...eeee", "timestamp": "2023-10-27T10:01:00.000Z", "reason": "Resolve-tx-failed: PoolIsFull" }
The HTTP REST API provides access to the current state of blockchain resources. It follows RESTful conventions and returns JSON responses.
- Base URL:
http://localhost:3000/api/v1(development) - Content-Type: All requests and responses use
application/json - CORS: Enabled for all origins (should be restricted in production)
Retrieves the most recent block information.
- Endpoint:
GET /api/v1/blocks/latest - Description: Returns the latest confirmed block with basic information.
Response:
{
"data": {
"blockNumber": "12345678",
"blockHash": "0x...ffaa",
"timestamp": "2023-10-27T10:00:00.000Z",
"transactionCount": 45,
"totalFees": "2500000"
}
}Example Request:
GET /api/v1/blocks/latest
Retrieves detailed information for a specific block.
- Endpoint:
GET /api/v1/blocks/{blockNumber} - Description: Returns detailed information for the specified block, including all transactions.
- Parameters:
blockNumber(path): Block number as a string of digits
Response:
{
"data": {
"blockNumber": "12345678",
"blockHash": "0x...ffaa",
"timestamp": "2023-10-27T10:00:00.000Z",
"miner": "ckb...xxxx",
"transactionCount": 45,
"transactions": [
{
"txHash": "0x...abc",
"fee": "10000",
"feeRate": "19.53",
"size": "512"
}
]
}
}Example Request:
GET /api/v1/blocks/12345678
Retrieves detailed information for a specific transaction.
- Endpoint:
GET /api/v1/transactions/{txHash} - Description: Returns complete transaction information including inputs, outputs, and current status.
- Parameters:
txHash(path): Transaction hash as a hex string starting with 0x
Response:
{
"data": {
"txHash": "0x...abcd",
"status": "pending",
"timestamp": "2023-10-27T10:00:05.123Z",
"fee": "10000",
"feeRate": "19.53",
"size": "512",
"cycles": "3000000",
"inputs": [
{
"previousOutput": {
"txHash": "0x...prev",
"index": "0"
},
"since": "0x0"
}
],
"outputs": [
{
"capacity": "50000000000",
"lock": {
"codeHash": "0x...lock",
"hashType": "type",
"args": "0x..."
}
}
]
}
}Transaction Status Values:
pending: Transaction is in mempool but not yet proposedproposed: Transaction is included in a block's proposalsconfirmed: Transaction is confirmed on-chain
Example Request:
GET /api/v1/transactions/0x...abcd
Retrieves a complete snapshot of the current blockchain state for initial page loading.
- Endpoint:
GET /api/v1/snapshot - Description: Returns the current state including latest block, pending transactions, and proposed transactions in a single request for efficient initial page loading.
Response:
{
"data": {
"latestBlock": {
"blockNumber": "12345678",
"blockHash": "0x...ffaa",
"timestamp": "2023-10-27T10:00:00.000Z",
"transactionCount": 45
},
"pendingTransactions": [
{
"txHash": "0x...abc",
"timestamp": "2023-10-27T10:00:05.123Z",
"fee": "10000",
"feeRate": "19.53",
"size": "512"
}
],
"proposedTransactions": [
{
"txHash": "0x...def",
"timestamp": "2023-10-27T10:00:10.456Z",
"fee": "15000",
"feeRate": "22.06",
"size": "680",
"context": {
"blockNumber": "12345678",
"blockHash": "0x...ffaa"
}
}
]
},
"timestamp": "2023-10-27T10:00:20.000Z"
}Example Request:
GET /api/v1/snapshot
All successful API responses follow this structure:
data: The main payload containing the requested resource(s)timestamp(for snapshot endpoint): Server timestamp when response was generated
Error responses are handled by a global exception filter and follow this structure:
{
"statusCode": 404,
"message": "Block 12345678 not found",
"timestamp": "2023-10-27T10:00:20.000Z",
"path": "/api/v1/blocks/12345678"
}200 OK: Request successful400 Bad Request: Invalid request parameters (e.g., invalid block number format, invalid transaction hash format)404 Not Found: Resource not found (e.g., block or transaction not found)500 Internal Server Error: Server error