All Articles

Building APIs quickly

— — — —

In this post, I have outlined when to use low code platforms to build APIs. Additionally I have compared some popular frameworks like express, nestJS, springboot, hasura and prisma which could be used to build APIs. Finally, I have presented the reasons why I like using Prisma and Hasura in tandem to build APIs

— — — —

Ways to build APIs

There are a few different ways to build APIs quickly, but the approach you should take will depend on your specific needs and requirements. Here are a few options to consider:

  • Use a low-code platform: Low-code platforms are tools that allow you to build applications without writing a lot of code. These platforms often come with pre-built components and templates that you can use to create your API quickly.
  • Use a framework: Many programming languages come with frameworks that make it easier to build APIs. For example, if you’re using JavaScript, you could use a framework like Express to build your API.
  • Use a managed API service: If you don’t want to worry about managing the infrastructure for your API, you can use a managed API service like AWS Lambda or Azure Functions. These services let you focus on building your API without worrying about infrastructure.

Problem with Low code platforms

Of the options listed above, using a low-code platform is likely the fastest way to build an API. The main disadvantage of using a low-code platform is that you may be limited in terms of the functionality and flexibility of your API. Additionally, low-code platforms can be more expensive than building your API from scratch.

Another disadvantage of using a low-code platform is that you may not have as much control over the underlying infrastructure as you would if you built your API from scratch. This can make it more difficult to scale your API and ensure its performance.

Low code platforms limit flexibility & customizations

Low-code platforms typically come with a library of pre-built components and templates that you can use to build your application quickly. These components and templates are often designed to be easy to use and to cover a wide range of common use cases.

For example, a low-code platform might include a template for building a basic login page. This template would include the necessary code and user interface elements to create a login page, as well as any required backend logic for handling user authentication.

While these pre-built components and templates can make it easier to get started with building your application, they can also limit your ability to fully customize your application. For example, if the pre-built login template doesn’t include a specific feature that you need, you may not be able to easily add that feature to your application.

Also, low-code platforms may not provide as many options for querying, updating, and manipulating your data as a custom-built application. This can make it more difficult to work with your data in the way that you need to.

Overall, the pre-built components and templates of low-code platforms can be a useful starting point for building your application, but they may not offer the same level of flexibility and customization as building your application from scratch.

Building APIs using frameworks

Advantages of using a framework to build an API

Assuming a low code platform doesn’t fit the need. Using a framework is likely the next fastest way to build an API for a custom application. This is because frameworks provide a pre-built set of tools and components that you can use to quickly build your API.

For example, if you’re using JavaScript, you could use a framework like Express to build your API. Express is a popular and well-documented framework that makes it easy to build APIs quickly and efficiently. It includes tools for routing, handling requests and responses, and working with data, among other things.

Using a framework like Express can save you a lot of time and effort compared to building your API from scratch, because you don’t have to write all of the necessary code yourself. This can allow you to focus on building the unique features and functionality of your API, rather than spending time on boilerplate code.

API layer & Database layer

In an API, the API layer is the part of the application that defines the interface for interacting with the data and functionality provided by the API. The API layer is responsible for defining the endpoints, parameters, and other details that determine how clients can access and manipulate the data and functionality provided by the API.

The database layer, on the other hand, is the part of the application that manages the data stored in the database. The database layer is responsible for connecting to the database, executing SQL queries, and managing the data in the database. It provides the underlying data storage and management capabilities that are used by the API layer to provide access to the data and functionality provided by the API.

The two layers work together to provide a consistent and predictable way for clients to access and manipulate the data and functionality provided by the API.

Separating database layer from api layer using prisma

Many frameworks do not provide any explicit separation of the API layer from the database layer. The separation is left to the developer to implement while coding the API. Using tools like Prisma, we can easily separate the database layer from the api layer. Which can help keep the code clean - especially while trying to do rapid API development.

Prisma is an open-source database toolkit that provides a set of tools and libraries for working with databases in a modern and type-safe way. It is designed to be used as part of the database layer of an application, and it provides a set of features and capabilities that make it easier and more convenient to work with databases in a modern and type-safe way. Prisma generates an SDK based on the database schema. This SDK can then be invoked by any javascript web frameworks (NestJs, Express etc)

Separating database layer from api layer using other frameworks

Nest.js provides an explicit separation of the API layer from the database layer using the concept of repositories. A repository is a class that defines a set of methods for accessing and manipulating data in the database. The API layer can then use these methods to access and manipulate the data without needing to know the details of the underlying database structure or SQL queries.

LoopBack and Django are frameworks that provide a mechanism to separate the database layer from the API layer include:

LoopBack: LoopBack is a web framework for building APIs and microservices using Node.js. It provides an explicit separation of the API layer from the database layer using the concept of models. A model is a class that defines the schema of a database table, as well as methods for accessing and manipulating the data in that table. The API layer can then use these methods to access and manipulate the data without needing to know the details of the underlying database structure or SQL queries.

Django REST framework: Django REST framework is a web framework for building APIs. It provides an explicit separation of the API layer from the database layer using the concept of serializers and views. Serializers are classes that define the structure of the data that is passed between the API layer and the database layer, and views are classes that define the endpoints and other details of the API. The API layer can then use these classes to access and manipulate the data without needing to know the details of the underlying database structure or SQL queries.

Comparing Web Frameworks

Express vs NestJs

NestJS is another popular framework for building web applications and APIs. It is a framework for building scalable, efficient, and modular server-side applications in JavaScript. It is built on top of the popular Express.js web framework, and it uses TypeScript, a typed superset of JavaScript, to provide strong typing and modularity. NestJs provides a wide range of tools and libraries for building APIs, including support for routing, middleware, and data validation. NestJS is built using TypeScript and follows a modular architecture, while Express is built using JavaScript and has a more traditional middleware-based architecture.

Another key difference is the level of abstraction provided by the two frameworks. NestJS provides a higher level of abstraction than Express, which means that it can be easier to use and more opinionated. This can make it a better choice for developers who want a more structured and well-defined way of building their applications.

It’s difficult to say definitively which framework is faster for building APIs, as the speed of development will depend on a variety of factors, including the complexity of your API, the experience of the developer, and the tools and resources available.

That said, in general, NestJS may be slightly faster for building APIs than Express, because it provides a higher level of abstraction and is more opinionated. This can make it easier for developers to quickly get started with building their API, and can reduce the amount of boilerplate code that needs to be written.

Java Springboot vs NestJS

Java SpringBoot and NestJS are both popular frameworks for building web applications and APIs. They have some similarities, but there are also some key differences between the two.

One of the main differences between SpringBoot and NestJS is the programming languages they are built with. SpringBoot is built using Java, while NestJS is built using TypeScript. This means that SpringBoot is a better choice for developers who are familiar with Java, while NestJS is a better choice for developers who are familiar with TypeScript.

Another key difference between the two frameworks is the architecture they use. SpringBoot uses a traditional Model-View-Controller (MVC) architecture, while NestJS uses a modular architecture that is inspired by Angular. This means that NestJS may be a better choice for developers who are familiar with Angular or who prefer a modular architecture.

The main difference between the modular architecture used by NestJS and the architecture used by SpringBoot is the way that the different components of an application are organized and interact with each other.

In the case of SpringBoot, the architecture of an application can vary depending on the specific needs of the project. SpringBoot provides a number of different architectural styles and patterns that developers can choose from, including the traditional Model-View-Controller (MVC) architecture and other architectures like microservices and reactive programming.

NestJs also supports the microservices and reactive programming styles and in addition uses a modular architecture that is inspired by Angular. This architecture organizes an application into a hierarchy of components, where each component is responsible for a specific part of the application and has a well-defined interface for communicating with other components. This can make it easier to reuse and organize code, and can make it simpler to build large and complex applications.

NestJs vs Hasura

Hasura is a low code platform for developers, whereas NestJS is a web framework. NestJs and hasura can work together in tandem. NestJs can be the layer above Hasura, which takes care of the business logic and invariants. Hasura can handle the database layer.

Hasura is an open-source platform for building APIs that access and manipulate data in a database. It is built on top of GraphQL - a query language and runtime environment for APIs. Hasura automatically generates the necessary API endpoints for accessing and modifying data in a database.

Hasura can introspect a database and generate the necessary API endpoints for accessing and modifying the data in that database. This is one of the primary features of Hasura, and it makes it a powerful and convenient platform for building APIs that access and manipulate data in a database.

When you connect Hasura to a database, it automatically inspects the schema of the database and generates the necessary API endpoints for accessing and modifying the data. This allows you to quickly and easily build an API that provides access to the data in the database, without having to manually define the API endpoints or write any code.

Business logic in Hasura

Hasura was not designed to handle business logic, but it can now as an after thought.

Actions in Hasura are a way to extend the functionality of an API by adding custom logic. Actions are similar to functions or methods in programming languages, and they allow you to define custom logic that can be executed by the API in response to certain events or requests.

For example, you can use actions in Hasura to implement business logic, such as data validation or calculations, that cannot be handled by the built-in features of the platform. You can also use actions to integrate with other external services or APIs, such as sending an email or making a payment.

Building APIs quickly using Prisma & Hasura

An API can be built pretty quickly using Prisma and Hasura.

Prisma can be used to define the schema of the database and generate the tables and columns in the database, and Hasura can be used to introspect the database and automatically generate the API based on the schema of the database. This can provide a convenient and powerful way to quickly build an API that provides access to the data stored in your database.

Prisma has an SDL language which can be used to describe the database schema and consequently generate the tables and columns in the database.I shall make a separate post detailing this.

Published Dec 8, 2022

I am a software developer and more recently a generative AI consultant. I am passionate about connecting applications to generative AI. Please reach out, if you need to integrate generative AI into your application workflows.