Content management systems (CMSs) have been around for quite some time. Strapi is a headless CMS for Node.js that provides a GUI for creating different content types and user management baked into the platform. It supports both RESTful API and GraphQL.

In addition, Strapi supports both NoSQL and SQL databases. Changing the database is as simple as changing environment variables.

In this guide, we’ll show you how to create an API with Strapi.

Setting up the work environment

Strapi requires Node.js installed on the system. It provides a boilerplate generator, create-strapi-app, for setting up the application. It can be installed globally using npm with the following command.

$ npm i -g create-strapi-app

Using create-strapi-app is simple; just pass the name of the project. --quickstart will create a project with a default setting.

create-strapi-app my-blog --quickstart

You must create an admin user before using Strapi. The command npm run develop starts the server on http://localhost:1337. The admin user is created using http://localhost:1337/admin/auth/register.

Create Admin User

Once the boilerplate is ready, admin UI can be used to build the database schema for the API.

Backend

Strapi provides easy UI for the creation of database schema. For changing configuration, we have to edit out project files. For example, for changing the env variable we have to edit the config/environments folder.

Creating database schema

Strapi includes a content builder plugin that provides a great UI for creating a database schema. The plugin is independent of the database. The same schema can be used in the SQL and NoSQL databases.

The demo website will have a Blog Collection type and Comment Collection type. The blog will store most of the content and the comment collection will store comments on the blog and user information.

Creating a collection

Start by logging into the admin at http://localhost:1337/admin. Open the Content-Types Builder by clicking the corresponding button on the sidebar.

Content Types Builder Page

Now create a new collection named “Blog” to store blogs for the site. It will have the title, image, and content.

Blog Collection

Next, create a collection called “Comment.” This will store comments for the blog and include the content, user, and blog. The blog field stores a link to the corresponding blog and details about the user who created a given comment.

Comment Collection Blog Field

Comment collection user field

We’ve created links for comments: one to the user collection and other to the blog collection. The blog and user collections don’t have information about the link. Now our backend is all set.

Documentation plugin

We’ll install the documentation plugin from the Marketplace section for easy access to API details. This plugin will create the swagger specification for the API.

Plugins Marketplace

Authentication

Authentication is an important element of any application. Strapi has JWT-based authentication out of the box.

A default key is used for signing JWT. A signing key can be changed in the configuration file /extensions/users-permissions/config/jwt.json. The API for user signup and login is already baked into the platform.

{
  "jwtSecret": "f1b4e23e-480b-4e58-923e-b759a593c2e0"
}

We’ll use the local provider for authentication. This password and email/username are used to authenticate the user. If we click on “Documentation” from the sidebar, it will provide an option to see the swagger API documentation.

API Documentation

Click “Open the Documentation” to view the swagger API Documentation. Navigate to “UsersPermissions- User” to access the API to create a user and login user.

UserPermissions User

We’ll use /auth/local and /auth/local/register.

#node #strapi #api #programming

How to create an API with Strapi
5.40 GEEK