Introduction to RESTFul API
Quoted from the amazon website, RESTful API is an interface used by two computer systems to securely exchange information over the internet. Most business applications must communicate with other internal and third-party applications to perform various tasks. For example, to generate monthly payslips, your internal accounts system must share data with your customers’ banking systems to automate billing and communicate with internal time and attendance applications. RESTful APIs support this exchange of information because they follow software communication standards that are secure, reliable and efficient.
Before getting to how to implement it, do you actually know what REST is? REST stands for Representational State Transfer which is a software architecture that provides requirements on how APIs work. REST was originally created as a guide for managing communication on complex networks such as the internet. You can use a REST-based architecture to support high-performance and reliable communication at scale. You can easily deploy and modify it, bringing cross-platform visibility and portability to all API systems.
API developers can design APIs using several different architectures. APIs that follow the REST architecture style are referred to as REST APIs. Web services that implement the REST architecture are referred to as RESTful web services. The term RESTful API generally refers to a RESTful web API. However, you can use the terms REST API and RESTful API interchangeably.
Some principles of the REST architecture style:
- Uniform interface, fundamentally all RESTful web services have a standardized format. For example, the server can store data as text or as an HTML representation format.
- Statelessness*, which refers to the communication method where the server completes each client request independently. And the sever must fulfill all requests at all times.
- Layered System*, RESTFul runs on multiple sever with multiple layers such as security, application, business logic and work together to fulfill client requests.
- Cacheablility*, RESTFul supports caching of temporary storage processes for fast client response to improve response time.
- On-demand coding*, sever can temporarily extend or customize client functionality easily.
Some Things to Learn
Before we learn more about the RESTFul API, we need to first learn the previous topics below
- HTTP
- Golang Web
- Golang HTTPRouter
- JSON Golang
- Golang OpenAPI
Simple CRUD (Create, Read, Update, Delete) Application
In the current project, we will create a simple application project that can implement the RESTfull API. There are several criteria that will exist in this project application including:
- The data we will create is
Article
data which has attributes a. it (number) b. title (string) c. content (string) - This CRUD application we can a. create article data, b. make changes to article data, c. view article data and d. delete article data.
- All APIs that we will create need to add Authentication in the form of API-Key.
Some dependencies that we will use:
- MySQL driver: github.com/go-sql-driver/mysql
- HTTP Router: https://github.com/julienschmidt/httprouter
- Validation: https://github.com/go-playground/validator
- Environtment: https://github.com/joho/godotenv
Preparation
We will try to make a step-by-step preparation to create a RESTFul API as follows:
First we create the learn-golang-restful
folder with the command
mkdir learn-golang-restful && cd learn-golang-restful
Initialize the project by using Golang Module with the command below and then enter
go mod init github.com/santekno/learn-golang-restful
Next we take some dependencies that we will use by using go get
like this
go get -u github.com/lib/pq
go get -u github.com/julienschmidt/httprouter
go get -u github.com/go-playground/validator
Then two files will appear, namely go.mod
and go.sum
with file contents like this
go get -u github.com/lib/pq
go get -u github.com/julienschmidt/httprouter
go get -u github.com/go-playground/validator
Then two files will appear, namely go.mod
and go.sum
with file contents like this
module github.com/santekno/learn-golang-restful
go 1.21.1
require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator v9.31.0+incompatible // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.9 // indirect
)
Now we will create API documentation using OpenAPI, now for friends who have not installed the OpenAPI Editor extension in VSCode, we can install it first on this website https://marketplace.visualstudio.com/items?itemName=42Crunch.vscode-openapi.
Create a new file with the name apispec.yml
then fill in as below
openapi: '3.0.2'
info:
title: Article RESTFul API
description: API Spec for Article RESTFul API
version: '1.0'
servers:
- url: https://localhost:8080/