Saturday, 17 October 2026

Blockchain Meets DRM: Building a Simple FastAPI

Demonstrator for Protected API Content

What happens when you combine a blockchain ledger with API-based content protection? While blockchain won't stop users from copying data once they've received it, it can provide a transparent, tamper-resistant way to manage digital rights, licences, and access history. In this article, we'll build the conceptual foundations for a simple FastAPI demonstrator that shows exactly how this works.


The Problem with Traditional DRM

Most Digital Rights Management (DRM) systems rely on a central database to determine whether a user is authorised to access content.

A typical flow looks like this:

User
  │
  ▼
API
  │
  ▼
Licence Database
  │
  ▼
Protected Content

This approach works, but it has several shortcomings:

  • Administrators can modify licence records
  • Audit trails can be incomplete
  • Ownership history may be difficult to verify
  • Multiple content providers require complex integration

For a demonstrator or educational project, a blockchain-inspired ledger offers an interesting alternative.


What Blockchain Adds

Instead of storing licence information in mutable database tables, every licence-related activity is recorded as an immutable event.

Examples include:

LICENCE_CREATED
LICENCE_REVOKED
CONTENT_ACCESSED
DOWNLOAD_LIMIT_REACHED

Each event becomes a block in a chain:

Genesis Block
      │
      ▼
Licence Created
      │
      ▼
Content Accessed
      │
      ▼
Licence Revoked

Every block contains the hash of the previous block.

{
  "index": 2,
  "timestamp": "2026-10-06T12:00:00Z",
  "previous_hash": "abc123",
  "event": {
    "type": "CONTENT_ACCESSED"
  },
  "hash": "def456"
}

If somebody alters a historical record, all subsequent hashes become invalid.

The API can therefore detect tampering by validating the chain.


The Demonstration Scenario

Imagine we're protecting a set of premium reports exposed through an API.

Available content:

report-001
report-002
report-003

Users must possess a valid licence before retrieving a report.

The blockchain stores:

  • Who owns a licence
  • Which content it applies to
  • When it expires
  • Whether it has been revoked
  • How many times it has been used

The content itself remains outside the blockchain.


Solution Architecture

Our FastAPI application contains four main components.

+------------------+
| FastAPI API      |
+--------+---------+
         |
         v
+------------------+
| DRM Service      |
+--------+---------+
         |
         v
+------------------+
| Blockchain       |
| Ledger           |
+--------+---------+
         |
         v
+------------------+
| Content Store    |
+------------------+

FastAPI

Provides REST endpoints.

Examples:

POST /licenses
GET  /content/{id}
POST /licenses/{id}/revoke
GET  /blockchain

DRM Service

Contains the business rules:

  • Validate ownership
  • Check expiry
  • Verify revocation status
  • Enforce download limits

Blockchain Ledger

Records every event.

The ledger acts as the system of record.

Content Store

Contains the protected content being delivered through the API.


How a Protected Request Works

When a client requests content:

GET /content/report-001?licenseId=LIC-100

The API performs several checks:

Step 1: Validate Blockchain

if not blockchain.is_valid():
    raise ChainIntegrityException()

The chain must be intact.


Step 2: Verify Licence

license = drm_service.find_license(
    "LIC-100"
)

The licence must exist.


Step 3: Check Status

if license.status == "REVOKED":
    raise AccessDenied()

Revoked licences cannot be used.


Step 4: Check Expiry

if license.is_expired():
    raise AccessDenied()

Expired licences are rejected.


Step 5: Record Access

blockchain.add_event({
    "type": "CONTENT_ACCESSED",
    "licenseId": "LIC-100"
})

Access is permanently recorded.


Step 6: Return Content

return report

The content is finally returned to the user.


Why This Is Useful

This approach introduces several concepts that are valuable for developers learning modern API architecture.

Immutable Audit Trail

Every important event is preserved.

Licence Created
Licence Accessed
Licence Accessed
Licence Revoked

Nobody can silently alter the history without breaking chain validation.


Transparent Usage Tracking

The blockchain forms a complete record of activity.

For example:

{
  "type": "CONTENT_ACCESSED",
  "user": "alice",
  "content": "report-001"
}

This makes reporting straightforward.


Download Limits

A licence can specify a maximum number of downloads.

{
  "licenseId": "LIC-100",
  "maxDownloads": 5
}

The service counts access events and denies further requests after the limit is reached.

Downloads: 5/5
Access Denied


Revocation History

Unlike traditional systems where a row is simply updated, blockchain records the revocation event itself.

Block 14
LICENCE_REVOKED

This preserves a complete history of changes.


What Blockchain Does Not Solve

This is the most important thing to understand.

A blockchain-based DRM system cannot stop copying after delivery.

Once the API returns content:

API
 │
 ▼
User

The user can still:

  • Save the response
  • Export the file
  • Screenshot the content
  • Record the screen
  • Redistribute information

Blockchain helps answer:

"Was this user authorised to access the content?"

It does not answer:

"Can we prevent this content from ever being copied?"

That is a fundamentally different problem.


Why FastAPI Is a Great Fit

FastAPI is particularly suitable for this demonstrator because it provides:

  • Automatic OpenAPI documentation
  • Strong typing
  • Pydantic validation
  • Lightweight architecture
  • Rapid prototyping

A basic endpoint can be implemented in only a few lines:

@app.get("/content/{content_id}")
def get_content(
    content_id: str,
    license_id: str
):
    return drm_service.retrieve(
        content_id,
        license_id
    )

This allows the focus to remain on DRM concepts rather than framework complexity.


Educational Value

Although simple, this project demonstrates several useful software engineering concepts:

AreaConcepts Demonstrated
APIsREST, validation, error handling
SecurityAuthorization, licence checking
BlockchainHashing, immutability, chain validation
ArchitectureSeparation of concerns
AuditingEvent sourcing concepts
TestingBehavioural and integration testing

For students, architects, and developers, it provides a practical way to explore blockchain ideas without the complexity of cryptocurrency networks or smart contracts.


Final Thoughts

Blockchain is often viewed through the lens of cryptocurrencies, but its most accessible value may be as an immutable audit ledger. A simple FastAPI-based DRM demonstrator is an excellent way to explore this idea.

By recording licence creation, revocation, and content access events on a lightweight blockchain, we gain transparency, traceability, and tamper detection while keeping all content delivery within a familiar REST API architecture.

It won't prevent piracy, but it does provide a clear, auditable answer to a crucial question:

Who was authorised to access what content, and when?

An example implementation can be found on my github.

No comments:

Post a Comment