CRUD (Create, Read, Update, and Delete) is a set of operations allowing users to interact with a database simply and consistently. Creating a CRUD API allows other systems and developers to interact easily with a data store or set. By providing a simple and consistent set of operations, CRUD APIs make it easy to build, scale, and secure applications.
Some benefits of a CRUD API include
- Simplified integration with a data set or application
- Improved security
- More control over how data is used, accessed and modified
- Easy scalability
When developing a CRUD API, there are many options to choose from. Low-code platforms are often a good choice as they tackle many notorious issues associated with the development and hosting while reducing the engineering time and resources required.
This post will explore how you can quickly create a CRUD API.
Read more: How to create comprehensive low-code APIs.
Define your endpoints
Linx allows for creating APIs in two main ways. A code-first API approach whereby you define your API’s events (endpoints) via a wizard. For more control and complexity, you can create a design-first API, where you create the API definition (Open API 3.0), import it to Linx, and add functionality to each event.
This post will focus on creating a simple API to manage product records in a SQL database.
You can use the SimpleRESTHost function to easily create APIs by defining the events (endpoints) with a wizard. For example, you can create an API with endpoints to facilitate the CRUD functions as illustrated below:
When the SimpleRESTHost is set up, the endpoints will be created as events. You can populate the required logic for each event.
Add the functionality to each endpoint.
Each event will require a slightly different set of features and functionality. The example focuses on interacting with data in a database. For this, you can use the Database plugin, which will allow you to execute simple or complex SQL queries, interact with MongoDB databases, and more.
To create a new product, insert the values received in the request body into the target table. Adding specific logic to ensure that values are valid can be done by using If-Else functions and conditional statements. Inserting into a database can be done by using the ExecuteSQL function. You can generate Select, Update and Insert statement templates with Linx.
The below function will validate that the product name is not null and that the price is more than zero. It will then insert the new product into a table and send a relevant message upon completion.
The Read endpoint will simply retrieve the complete list of products by using a function that reads from a database to return all the records as a list. The list of records will be sent back as the response body.
Additional functionality can be added if you create a paging model (when there is too much data to send back in one response body). Additional events can also be created if you need to search for a specific record. For example, an endpoint that filters all products for a match on the name. For this, you can add a filter parameter and then apply the filter in the SQL statement.
Similar to the Create event, the message body will contain all values to be updated as well as the identifier, this will be used in the Update statement to update the specific record. It will also help to add functionality that will check if an invalid ID has been passed in. This can be added either in the SQL statement or you can build the functionality outside of the SQL statement.
You may also choose to create stored procedures to handle the database interactions. Stored procedures can be executed by using the Execute Stored Procedure function.
The delete method may be simple, as you can use a Delete statement that deletes the record with a passed-in ID. You can implement more complex logic to ensure that only authorised users can access this functionality. This restriction can be set up by using and setting up a specific security scheme.
Security operations can be as simple or as complex as you need them.
Testing your API
You can test your API by using the built-in Debugger. Debugging will create a locally hosted version of the API, meaning you can call it if you have set the Base URI to something like ‘http://localhost:8080/products’.
Linx can host Swagger or Redocly documentation for you. To do this, select the type of documentation you need in the API Documentation setting of your SimpleRESTHost. This documentation will show you how you can call the API. You can even execute the methods from the documentation.
Another useful feature is that you can get the OpenAPI specification to import it elsewhere.
Get your API up and running
When testing is completed, you can deploy your API to be hosted on a Linx server. Deploying to a Linx server simplifies deployment, hosting and monitoring of your API and applications.
The Server is a Windows Service that hosts and manages your Linx Solutions and web services. Once the application is deployed, you must turn the REST Service on to start using the CRUD API. Give the end-points, or the endpoint to the hosted documentation, to your users, and they can continue with their implementations.