Applied8 min readLesson 4 of 5
Calling external APIs from COBOL
APIs work in both directions. A COBOL program can call a service elsewhere — a fraud check, an address lookup, a cloud service — by building JSON, sending an HTTP request, and reading the reply.
Three common ways
- z/OS Connect API requester — generates COBOL copybooks from the other service's OpenAPI document. Your program fills in a structure and makes a call; z/OS Connect handles HTTP and JSON.
- CICS web API — a CICS program opens a connection with
EXEC CICS WEB OPEN, sends the request withWEB CONVERSE, and reads the response. - COBOL JSON statements — Enterprise COBOL can build JSON from a data structure with
JSON GENERATEand read it back withJSON PARSE, whatever transport you use.
* WS-FRAUD-REQ holds account id and amount JSON GENERATE WS-JSON-OUT FROM WS-FRAUD-REQ COUNT IN WS-JSON-LEN NAME OF ACCT-ID IS 'accountId' NAME OF TXN-AMOUNT IS 'amount' ON EXCEPTION PERFORM 9000-JSON-ERROR END-JSON
Plan for the other side failing
The external service is outside your control. It will sometimes be slow or unavailable, and your transaction must still behave well.
- Set a timeout. A CICS transaction waiting forever holds resources and can affect other users.
- Decide the fallback. If the fraud check is down, do you refuse the payment, allow it under a limit, or queue it for review? This is a business decision — get it written down.
- Retry carefully. Only retry calls that are safe to repeat (idempotent). Retrying a payment could pay twice.
- Log enough to trace. Record a correlation ID, the status code and the time taken, never the full personal data.
Common mistakes
No timeout
One slow external service can tie up CICS tasks until the region struggles. Always set a timeout and handle it.
Ignoring the status code
Parsing the body without checking for 200 first leads to garbage data being processed as if it were valid.
Retrying non-idempotent calls
A retried payment or order can happen twice. Use a unique request ID so the other side can detect duplicates.
What you will see at work
- Outbound calls usually go through an outbound proxy or gateway; network and firewall teams will be involved in the first setup.
- TLS certificates expire. An expired certificate on a partner's side shows up as connection failures in your program.
- Monitoring dashboards that show external call latency help answer 'is it us or them?' during incidents.
Key terms
Check your understanding.
Take this lesson's quiz and save your progress. Free.