# hyperledger/fabric-contract-api-go architecture

> Architecture diagram of the GitHub repository hyperledger/fabric-contract-api-go, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hyperledger/fabric-contract-api-go
- Repository: https://github.com/hyperledger/fabric-contract-api-go

## Overview

Hyperledger Fabric Go Contract API is a runtime library for exposing Go contract methods as Fabric chaincode transactions. A client proposal reaches the chaincode entry point, which selects a contract and transaction handler; contract methods use a transaction context to access ledger state, and results are serialized for the response. The library also provides reflection-based contract and type validation, transaction hooks, and chaincode metadata/schema support. Several implementation links are evidenced by the source index or excerpts; links for unsampled invocation details are omitted.

Main entry points: [`contractapi/contract_chaincode.go`](https://github.com/hyperledger/fabric-contract-api-go/blob/main/contractapi/contract_chaincode.go) (Chaincode entry), [`internal/contract_function.go`](https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/contract_function.go) (Contract functions), [`metadata/metadata.go`](https://github.com/hyperledger/fabric-contract-api-go/blob/main/metadata/metadata.go) (Chaincode metadata), [`internal/types/types.go`](https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/types/types.go) (Type definitions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Chaincode runtime"]
  node_chaincode["Chaincode entry"]
  node_system["System contract<br/>[system_contract.go]"]
  node_context["Transaction context"]
  node_hooks["Transaction hooks"]
end

subgraph group_contracts["Contract model"]
  node_contract["Contract API<br/>[contract.go]"]
  node_function["Contract functions"]
  node_typecheck["Type validation<br/>[types_handler.go]"]
  node_types["Type definitions<br/>[types.go]"]
  node_private["Private contract example<br/>[main.go]"]
  node_sample["State contract example<br/>[main.go]"]
end

subgraph group_metadata["Metadata"]
  node_metadata["Chaincode metadata<br/>[metadata.go]"]
  node_schema["Metadata schema<br/>[schema.go]"]
end

subgraph group_serialization["Serialization"]
  node_serializer["Serializer interface"]
  node_json["JSON serializer"]
end

node_client(("Fabric client"))
node_ledger[("Fabric ledger")]

node_client -->|"submits transaction"| node_chaincode
node_chaincode -->|"adds system contract"| node_system
node_chaincode -->|"reflects metadata"| node_metadata
node_chaincode -->|"uses handlers"| node_function
node_chaincode -->|"configures hooks"| node_hooks
node_chaincode -->|"sets serializer"| node_serializer
node_contract -->|"uses metadata"| node_metadata
node_function -->|"validates types"| node_types
node_function -->|"uses schemas"| node_metadata
node_function -->|"formats values"| node_serializer
node_typecheck -->|"checks types"| node_types
node_hooks -->|"builds handler"| node_function
node_metadata -->|"validates against"| node_schema
node_schema -->|"uses types"| node_types
node_json -->|"converts values"| node_types
node_json -->|"validates schemas"| node_metadata
node_json -->|"implements"| node_serializer
node_sample -->|"starts chaincode"| node_chaincode
node_private -->|"starts chaincode"| node_chaincode
node_sample -->|"reads and writes"| node_ledger
node_private -->|"uses private data"| node_ledger
node_context -.->|"provides stub access"| node_ledger

click node_chaincode "https://github.com/hyperledger/fabric-contract-api-go/blob/main/contractapi/contract_chaincode.go"
click node_system "https://github.com/hyperledger/fabric-contract-api-go/blob/main/contractapi/system_contract.go"
click node_context "https://github.com/hyperledger/fabric-contract-api-go/blob/main/contractapi/transaction_context.go"
click node_contract "https://github.com/hyperledger/fabric-contract-api-go/blob/main/contractapi/contract.go"
click node_function "https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/contract_function.go"
click node_typecheck "https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/types_handler.go"
click node_types "https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/types/types.go"
click node_hooks "https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/transaction_handler.go"
click node_metadata "https://github.com/hyperledger/fabric-contract-api-go/blob/main/metadata/metadata.go"
click node_schema "https://github.com/hyperledger/fabric-contract-api-go/blob/main/metadata/schema.go"
click node_serializer "https://github.com/hyperledger/fabric-contract-api-go/blob/main/serializer/transaction_serializer.go"
click node_json "https://github.com/hyperledger/fabric-contract-api-go/blob/main/serializer/json_transaction_serializer.go"
click node_private "https://github.com/hyperledger/fabric-contract-api-go/blob/main/integrationtest/chaincode/private/main.go"
click node_sample "https://github.com/hyperledger/fabric-contract-api-go/blob/main/integrationtest/chaincode/simple/main.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_chaincode,node_system,node_context,node_hooks,node_client toneBlue
class node_contract,node_function,node_typecheck,node_types,node_private,node_sample,node_ledger toneAmber
class node_metadata,node_schema toneMint
class node_serializer,node_json toneRose
```

## Components

### Chaincode runtime

- [Chaincode entry](https://github.com/hyperledger/fabric-contract-api-go/blob/main/contractapi/contract_chaincode.go): `contractapi/contract_chaincode.go`
- [System contract](https://github.com/hyperledger/fabric-contract-api-go/blob/main/contractapi/system_contract.go): `contractapi/system_contract.go`
- [Transaction context](https://github.com/hyperledger/fabric-contract-api-go/blob/main/contractapi/transaction_context.go): `contractapi/transaction_context.go`
- [Transaction hooks](https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/transaction_handler.go): `internal/transaction_handler.go`

### Contract model

- [Contract API](https://github.com/hyperledger/fabric-contract-api-go/blob/main/contractapi/contract.go): `contractapi/contract.go`
- [Contract functions](https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/contract_function.go): `internal/contract_function.go`
- [Type validation](https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/types_handler.go): `internal/types_handler.go`
- [Type definitions](https://github.com/hyperledger/fabric-contract-api-go/blob/main/internal/types/types.go): `internal/types/types.go`
- [Private contract example](https://github.com/hyperledger/fabric-contract-api-go/blob/main/integrationtest/chaincode/private/main.go): `integrationtest/chaincode/private/main.go`
- [State contract example](https://github.com/hyperledger/fabric-contract-api-go/blob/main/integrationtest/chaincode/simple/main.go): `integrationtest/chaincode/simple/main.go`

### Metadata

- [Chaincode metadata](https://github.com/hyperledger/fabric-contract-api-go/blob/main/metadata/metadata.go): `metadata/metadata.go`
- [Metadata schema](https://github.com/hyperledger/fabric-contract-api-go/blob/main/metadata/schema.go): `metadata/schema.go`

### Serialization

- [Serializer interface](https://github.com/hyperledger/fabric-contract-api-go/blob/main/serializer/transaction_serializer.go): `serializer/transaction_serializer.go`
- [JSON serializer](https://github.com/hyperledger/fabric-contract-api-go/blob/main/serializer/json_transaction_serializer.go): `serializer/json_transaction_serializer.go`

### Other components

- **Fabric client**
- **Fabric ledger**

## Connections

- Fabric client → Chaincode entry: submits transaction (evidence: `contractapi/contract_chaincode.go`)
- Chaincode entry → System contract: adds system contract (evidence: `contractapi/contract_chaincode.go`)
- Chaincode entry → Chaincode metadata: reflects metadata (evidence: `contractapi/contract_chaincode.go`)
- Chaincode entry → Contract functions: uses handlers (evidence: `contractapi/contract_chaincode.go`)
- Chaincode entry → Transaction hooks: configures hooks (evidence: `contractapi/contract_chaincode.go`)
- Chaincode entry → Serializer interface: sets serializer (evidence: `contractapi/contract_chaincode.go`)
- Contract API → Chaincode metadata: uses metadata (evidence: `contractapi/contract.go`)
- Contract functions → Type definitions: validates types (evidence: `internal/contract_function.go`)
- Contract functions → Chaincode metadata: uses schemas (evidence: `internal/contract_function.go`)
- Contract functions → Serializer interface: formats values (evidence: `internal/contract_function.go`)
- Type validation → Type definitions: checks types (evidence: `internal/types_handler.go`)
- Transaction hooks → Contract functions: builds handler (evidence: `internal/transaction_handler.go`)
- Chaincode metadata → Metadata schema: validates against (evidence: `metadata/metadata.go`)
- Metadata schema → Type definitions: uses types (evidence: `metadata/schema.go`)
- JSON serializer → Type definitions: converts values (evidence: `serializer/json_transaction_serializer.go`)
- JSON serializer → Chaincode metadata: validates schemas (evidence: `serializer/json_transaction_serializer.go`)
- JSON serializer → Serializer interface: implements (evidence: `serializer/json_transaction_serializer.go`)
- State contract example → Chaincode entry: starts chaincode (evidence: `integrationtest/chaincode/simple/main.go`)
- Private contract example → Chaincode entry: starts chaincode (evidence: `integrationtest/chaincode/private/main.go`)
- State contract example → Fabric ledger: reads and writes (evidence: `integrationtest/chaincode/simple/main.go`)
- Private contract example → Fabric ledger: uses private data (evidence: `integrationtest/chaincode/private/main.go`)
- Transaction context → Fabric ledger: provides stub access
