← Back to list

API Enablement: Exposing Mainframe Functions Using Python-based APIs

In a world of microservices and cloud-native applications, mainframes remain the backbone for many critical business functions. Yet…

Sam Nathan in In a Byte Size · 2025-05-20 19:52 · 3 claps · 2.5 min read paywalled
#mainframe-api #python-to-mainframe #python-api-db2 #api-rest-python #python-fast-api
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud

API Enablement: Exposing Mainframe Functions Using Python-based APIs

In a world of microservices and cloud-native applications, mainframes remain the backbone for many critical business functions. Yet, integrating these legacy systems with modern digital platforms is often challenging. API enablement provides a bridge between the old and the new.

In this article, we’ll explore how to expose mainframe functions through Python-based REST APIs, making use of lightweight frameworks like Flask or FastAPI, while bridging communication with mainframe systems via MQ, CICS, or DB2.

Why API Enablement for Mainframes?

  • Modern Interfaces: Enable mobile apps, web portals, and external partners to access mainframe business logic via REST APIs.
  • Decouple UI from Mainframe: Simplify frontend development by providing RESTful interfaces.
  • Agile & Scalable: Faster development cycles with Python, containerization, and cloud deployment.
  • Security & Governance: Apply API-level security, monitoring, and throttling.

Architecture: Python API as Mainframe Facade

Frontend (Web/Mobile)
      |
   REST API (Flask/FastAPI)
      |
+---------------------------+
| Python Mainframe Adapter  |
| (MQ, CICS, DB2 Handlers)  |
+---------------------------+
      |
 Mainframe Business Logic

Tools & Technologies

  • Flask / FastAPI: For creating REST APIs.
  • IBM MQ (pymqi library): For messaging between Python and mainframe queues.
  • DB2 (ibm_db library): For direct database access.
  • CICS: Via MQ or HTTP bridge interfaces.
  • Security: OAuth2, JWT tokens for API protection.

Sample Use Case: Exposing Mainframe Customer Lookup as API

Scenario:

Expose a customer lookup service on mainframe (CICS/DB2) via a REST API for modern apps to consume.

Sample Code: Python FastAPI to DB2 Integration

Install Required Libraries:

pip install fastapi uvicorn ibm-db ibm-db-sa python-jose[cryptography]

Python API Code:

from fastapi import FastAPI, HTTPException, Depends
from jose import JWTError, jwt
import ibm_db
app = FastAPI()
# DB2 Connection Details
dsn = (
    "DATABASE=MYDB;"
    "HOSTNAME=mainframe.company.com;"
    "PORT=50000;"
    "PROTOCOL=TCPIP;"
    "UID=db2user;"
    "PWD=db2password;"
)
# JWT Secret Key
SECRET_KEY = "yoursecretkey"
ALGORITHM = "HS256"
# JWT Authentication Dependency
def verify_token(token: str):
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        return payload
    except JWTError:
        raise HTTPException(status_code=401, detail="Invalid token")
# Customer Lookup API
@app.get("/customer/{customer_id}")
def get_customer(customer_id: str, token: str = Depends(verify_token)):
    try:
        conn = ibm_db.connect(dsn, "", "")
        sql = f"SELECT * FROM CUSTOMERS WHERE CUSTOMER_ID = '{customer_id}'"
        stmt = ibm_db.exec_immediate(conn, sql)
        result = ibm_db.fetch_assoc(stmt)
        if not result:
            raise HTTPException(status_code=404, detail="Customer not found")
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    finally:
        ibm_db.close(conn)
# Run: uvicorn script:app --reload

Alternative: Mainframe via MQ (CICS)

If your mainframe logic is exposed via MQ queues:

  • Use pymqi to send/receive messages to/from mainframe queues.
  • Encapsulate request/response formats (e.g., JSON, COBOL copybook mappings).
  • Process MQ responses and expose via REST API.

Security Considerations

  • OAuth2 Integration: Use Identity Providers (Okta, Azure AD) for token generation.
  • JWT Validation: Implement token verification in API middleware.
  • Rate Limiting & Throttling: Prevent API abuse using API gateways (e.g., Kong, AWS API Gateway).
  • Audit Logging: Log API requests/responses for traceability.
  • Encryption in Transit: Ensure TLS/SSL is enforced between all components.

Real-World Use Cases

Python API mapping based on mainframe interface usecase

Python API mapping based on mainframe interface usecase

Benefits of Python-based API Enablement

  • Rapid Prototyping & Delivery
  • Ease of Maintenance & Scaling
  • Cloud & DevOps Friendly
  • Seamless Integration with Modern Apps
  • Strong Community Support (Flask, FastAPI)

Architecture based on API-enabled mainframe using python

Architecture based on API-enabled mainframe using python

Final Thoughts

API-enabling mainframes with Python bridges the gap between legacy systems and modern digital platforms. With lightweight frameworks like FastAPI and robust libraries for MQ and DB2, enterprises can quickly expose valuable mainframe business logic as secure, scalable APIs.

This approach not only extends the life of mainframes but also accelerates digital transformation initiatives.


메타데이터
post_id
d20931602bca
slug
api-enablement-exposing-mainframe-functions-using-python-based-apis-d20931602bca
url
https://medium.com/in-a-byte-size/api-enablement-exposing-mainframe-functions-using-python-based-apis-d20931602bca
canonical_url
https://medium.com/in-a-byte-size/api-enablement-exposing-mainframe-functions-using-python-based-apis-d20931602bca
author_url
https://medium.com/@sajivkamalakar
status
ok
fetched_at
2026-06-12 18:14:10