Consistent API design

Suppose we have an API /getTransaction with following response

{
transactionId: “TCC1232434”,
bankName: “ABC”,
accountNumber: 12131314,
transactionComments: "comments"
}

We might have another API to create the transaction and generally, transactionComments is an optional field

So while creating a transaction if someone does not enter transaction comments, nothing is stored in the database for field transactionCommentswhich is equivalent to null

and thus calling /getTransaction will return

{   
transactionId: “TCC1232434”,
bankName: “ABC”,
accountNumber: 12131314,
transactionComments: null
}

Now consumer of API is expecting transactionComments to be a string, and so might have string methods applied on transactionCommentslike this transactionComments.replace(' ‘,' — ‘)

Getting null as a value for transactionCommentsfrom API, breaks this frontend code

We could well apply null check at front-end, but the number of places where this null check has to be applied is incalculable, so this null check should be handled by API, not frontend code

Thus a valid API response in case transactionComments in database is null should be

{
transactionId: “TCC1232434”,
bankName: “ABC”,
accountNumber: 12131314,
transactionComments: null
}

So now when we have agreed over handling such checks at API level, we have to define a compatible null value as well

In our example ‘’ is a perfect null value for transactionComments and for other cases, we have to find a value which is not a valid value and define it as a null value like maybe 0 for account number assuming a valid account number can never be 0

The contract between front-end and backend is not broken due to value changes in the database

Please let me know your views.

--

--

--

I love learning

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The story behind 1.0

Testing the Performance of a Pricing Optimization Model — My practicum journey with Engage3

Install cockroachdb in kubernetes

Elasticsearch in Action: Overview of Mapping

External Ingress Control for Cloud Run

Why are Screen Space Reflections Unique?- Game Dev Series 167

UPDATE: PMDG 737 V2 & 747 Immersion now Prepar3D v5 Ready!

Master a Programming Language

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Jain

Akshay Jain

I love learning

More from Medium

Publish API Documentation Postman

Build CRUD on RESTful API with MongoDB — Javascript, Part4 — POST and CREATE.

REST services

SOLID Principles