Skip to content

CICS and mainframe transactions: how to expose them as APIs

B2Bnetwork team · · 6 min

IBM i, RPG and mainframe

In brief

You expose a CICS transaction as an API by calling, through a REST layer, the same COBOL program that a 3270 screen or other systems use today. The most common tool is IBM z/OS Connect, which maps JSON to COBOL structures and describes the API in OpenAPI; less often, teams use the built-in JSON services in CICS or their own Java application in CICS. The prerequisite is logic in a program called with LINK, separated from screen handling. After that, security, idempotency and performance decide the outcome.

Transaction, program and input data

In CICS, a user or system starts a transaction, which is a four-character identifier assigned to a program. Classic 3270 applications are pseudo-conversational: the program sends a screen (a BMS map), ends, and is started again when the user presses Enter. Such a program mixes logic with screen handling and is hard to call from outside.

A good API candidate is a program called with EXEC CICS LINK that receives input and returns a result without a screen. Data is passed in two ways:

MechanismHow it worksLimitation
COMMAREAA single storage area passed to the program and backAccording to IBM documentation, the recommended maximum is 24 KB and the absolute maximum is 32 KB
Channels and containersNamed blocks of data grouped in a channelContainers are not limited to 32 KB

For an API that returns, for example, a list of account transactions or a policy history, the COMMAREA limit quickly becomes a problem, so new interfaces are usually designed around channels and containers.

When the logic sits in a screen program

In many banking and insurance systems, the rules are written in programs that handle 3270 screens. There are two paths in that case. The lasting one is to extract the logic into a separate program called with LINK: the screen calls the new program just like the API does, so the rules live in one place. The quick one is to drive the transaction through the screen, using a tool that pretends to be a terminal user. It works without code changes, but it is slower and breaks with every screen change, much like the 5250 refacing tools described in the article 5250 screens and web interfaces.

Extracting logic works best one transaction at a time, starting with the one a new channel is waiting for, such as a mobile app or a partner. Each extracted transaction is protected by a test that compares the results of the old and new paths on the same data.

Three ways to expose an API

ApproachWhat it involvesWhen it makes sense
IBM z/OS ConnectAn API server on z/OS maps REST requests with JSON to CICS program calls, with the API described in OpenAPIMany APIs, a need for consistent descriptions and tooling, including calls from z/OS to the outside
JSON services in CICSThe JSON assistant (for example DFHLS2JS) generates a schema and binding for the program's structureIndividual services in an existing CICS setup
Java application in CICSYour own REST service in a JVM server in CICS calls programs with LINKWhen the API needs its own logic, for example combining several calls

In the CICS documentation, IBM points to z/OS Connect as its premier technology for JSON APIs for CICS and describes JSON web services in CICS itself as an older technology. An API gateway usually sits in front of z/OS and handles authentication, rate limits and versions. Its role is covered in the article API gateway: role, patterns and common configuration mistakes.

Designing an API on top of a transaction

The API should not be a copy of the COBOL structure. A few decisions are worth making up front:

  • Resources, not transactions. “GET /accounts/123/operations” rather than “POST /transaction/AB12.” An API client does not need to know CICS transaction IDs.
  • Data types. PIC X fields padded with spaces, packed decimals (COMP-3) and dates in YYYYMMDD format need to become ordinary JSON types. The mapping is easy to get wrong for signs and the number of decimal places.
  • Errors. Program return codes are translated into HTTP status codes and readable messages, without program or data set names in the response.
  • Idempotency. A transfer request that the client retries after a timeout must not post twice. The typical solution is an idempotency key stored together with the operation.
  • Unit of work. It is safest when one API call is one unit of work in CICS, committed or backed out as a whole. REST does not carry a distributed transaction across systems, so multi-step operations are designed with compensation.

Security, performance and testing

An API client usually authenticates with an OAuth 2.0 token at the gateway or in z/OS Connect. The identity from the token has to be mapped to a RACF user ID, so that CICS checks authority to transactions and resources and records who performed the operation. A shared technical ID for all calls simplifies configuration but blurs the audit trail. Token basics are covered in the article OAuth 2.0 and OpenID Connect demystified, and common mistakes in the article API security: the most common mistakes.

On the mainframe, performance has two dimensions: response time and the cost of processor capacity. A transaction designed for a few hundred operators may receive many times more calls from a mobile app. Reads that rarely change are worth caching in front of z/OS, and before you release the API, measure the load in a test environment.

Testing covers the API contract (does the response match the OpenAPI description), correct data mapping, edge cases and penetration testing. The project on penetration testing of an API for third-party providers shows an example of such tests for a new bank API, and we discuss when to modernize the mainframe more broadly in the article The mainframe in a bank: when to modernize and when to leave it.

Questions and answers

Do COBOL programs have to be rewritten to expose an API from CICS?

No, if the logic is in a program called with LINK using a COMMAREA or a channel. Such a program can be exposed right away through z/OS Connect or a CICS JSON service. Changes are needed when the logic is woven into a 3270 screen program. Then it is extracted into a separate program used by both the screen and the API.

What is IBM z/OS Connect?

It is IBM software for creating REST APIs for applications and data on z/OS, including CICS programs. It maps JSON requests to program data structures and describes APIs in OpenAPI format. It also works in the other direction: it lets COBOL programs on z/OS call external REST APIs, for example a scoring service or a partner's registry.

How do I avoid double posting when an API call is retried?

Through idempotency. The client sends a unique operation key with the request, and the system stores it together with the result in the same unit of work. When the same request arrives again, the system returns the stored result instead of performing the operation a second time. Without this, a network timeout and an automatic retry can duplicate a transfer or a policy.

How do I test an API exposed from CICS?

Like any API: with contract, functional tests with edge-case data, load and security tests. In addition, compare the result of an API call with the result of the same operation performed through the old path, because errors in mapping COBOL fields to JSON are the most common. Contract testing is described in the article API contract testing.

What it looks like in a project

Developing a system on IBM i or mainframe? Talk to an RPG engineer.

Let's talk

Read more: IBM i, RPG and mainframe