Building language agnostic APIs using Golang

Siva Gollapalli
3 min readMay 27, 2020

Building APIs is a regular day-to-day task for most of us. It may be related to web apps with frontends like React, Angular, or might be mobile apps. Whatever may be the nature of the apps sometimes we might code the same or similar kind of functionalities in different apps. Sometimes functionality could be simple and straight forward whereas sometimes it takes significant time depends upon the nature of the requirement. Depends upon the ecosystem in which we are developing we might have libraries which satisfy our requirement or we have to write on our own. For example, in the Ruby ecosystem if you want to build authentication, most of us end up using Devise or Doorkeeper. So, if you want to build authentication again in another rails app we just repeat the same process. Since some gems integration is straight forward and we might not need to add much code from our side.

But, recently in our Rails apps we are trying to integrate Stripe taxes. For that, Stripe provides APIs to maintain taxes. That tax could be VAT, Sales Taxes, or some custom tax. But we have to write CRUD operations to add, update or delete taxes from Stripe. Since we need to add the same functionality but for two different projects of two different clients. Since both are Rails applications, we can derive a gem and use it in both applications. But, what if, we have to develop the same functionality in different ecosystems like Node.js, Python, or Java. I can’t include my Ruby gem but I have to write a common library in that ecosystem. It Looks like we are repeating the same job in different ecosystems. Would there be a common ecosystem where I share the same code across different language platforms? Yes, we have a Binary language 😃. What? Binary language!!!

As we all know if we have a binary executable we can easily imbibe into any ecosystem. But, building a binary executable is itself a tedious task since we as humans can understand high-level languages only. So, what if, we write code in a high-level language and convert it into binary code? Would that work? Whoa, it looks like we have a solution to our problem. Yes, Golang is the best option in this scenario. As a matter of fact, Golang converts all our code into a binary executable and we can run that anywhere without any runtime just like any executable.

Here is the code snippet

Run Golang and Rails on ports 8080, 3000 respectively. Try to hit http://localhost:3000/stripe/taxes , this will proxy our request to the golang server which runs on8080 which internally hits stripe server and returns the list of taxes. If you notice, we are still in the Rails ecosystem but proxying our repeatable task to Golang. The same Golang binary can be used inside any eco-system since we made it language independent. So, if we have the same kind of functionality inside different projects of different ecosystems, we can build the same in Golang and can share across which reduces our project timeline.

If you like the article please like and share it. Any suggestions would be welcome.

--

--