Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Designing Web APIs with Strapi
Designing Web APIs with Strapi

Designing Web APIs with Strapi: Get started with the Strapi headless CMS by building a complete learning management system API

Profile Icon Khalid Elshafie Profile Icon Mozafar Haider
By Khalid Elshafie , Mozafar Haider
€12.99 €18.99
Book Feb 2022 310 pages 1st Edition
eBook
€12.99 €18.99
Print
€23.99
Subscription
Free Trial
Renews at €18.99p/m
Profile Icon Khalid Elshafie Profile Icon Mozafar Haider
By Khalid Elshafie , Mozafar Haider
€12.99 €18.99
Book Feb 2022 310 pages 1st Edition
eBook
€12.99 €18.99
Print
€23.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€12.99 €18.99
Print
€23.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Designing Web APIs with Strapi

Chapter 1: An Introduction to Strapi

This chapter gives an introduction to Strapi. First, we will explain what Strapi is and what are the benefits of using it in developing API (short for application programming interface). Then, we will see how to set up and prepare our development environment to work with Strapi. Afterward, we will create a simple API to help us get started with Strapi. Finally, we will have a quick look at the server scripts offered by Strapi to start and stop the development server.

These are the topics we will cover in this chapter:

  • What is Strapi?
  • Why use Strapi? (The benefits of Strapi)
  • Preparing the development environment
  • Creating a Strapi application
  • Understanding server scripts

What is Strapi?

Strapi is a headless content management system (CMS).

A CMS is a software application used for web development that allows users to create, edit, and publish content.

A traditional CMS, such as WordPress, tightly couples the frontend and the backend—that is, the structure of the content and how it is presented. However, unlike traditional CMSes, a headless CMS is entirely decoupled from the presentation layer. The term headless comes from separating the head (the frontend) from the body (the backend). A headless CMS does not care about how the contents get displayed; instead, it provides a content-first approach with an API to access and display the data in any format desired.

You can see the differences between a traditional CMS and a headless CMS in the following diagram:

Figure 1.1: Traditional CMS versus headless CMS

Figure 1.1: Traditional CMS versus headless CMS

"Strapi" is wordplay for Bootstrap API. As the name suggests, it aims to help developers build (bootstrap) an API quickly. Strapi saves API development time through an integrated easy-to-use admin panel and a solid set of core features out of the box. Whether you are a backend, full stack, or frontend developer, you will find it extremely easy to get started with Strapi and API development without reinventing the wheel and wasting time in building common features such as basic create, read, update, and delete (CRUD) operations or authentication and authorization.

Now that we have familiarized ourselves with Strapi, let's see some of the advantages of using it.

Why use Strapi? (The benefits of Strapi)

There are several advantages to using Strapi, and we will outline some of the major benefits and reasons for using it next.

Open-source Node.js

Strapi is open-source and built on top of a popular Node.js framework: Koa. Its code is accessible and easily extendible. It's supported by a thriving company but also by a large community of contributors. You can find Strapi's code on GitHub at https://github.com/strapi/strapi.

Database-agnostic

Strapi can work with different database systems. It can be set up and configured to work with PostgreSQL, MySQL, MongoDB, and SQLite. We will see in Chapter 9, Production-Ready Applications, how we can configure Strapi to work with PostgreSQL.

Customizable (extendable)

Strapi is highly configurable to suit each project's specific requirements. All of the data types are created from scratch through the admin panel. Additionally, the Strapi plugin system makes it easy to extend its functionality with features such as database documenting, image uploads, and email configuration. We will discuss the Strapi plugin system in a later chapter of this book.

RESTful and GraphQL

Strapi provides a REpresentational State Transfer (REST) API out of the box. The API can be consumed from any web client (React, Angular, Vue.js, and so on), mobile applications, or even internet of things (IoT) applications using REST or GraphQL via a plugin.

Users and permissions

Strapi comes with a users and permissions model out of the box that allows you to define which endpoint is available for which user/role. Additionally, you can use Open Authorization (OAuth) to enable authentication via third-party providers such as GitHub, Facebook, Twitter, Google, and many others.

Now that we have understood what Strapi is all about, let's see how we can prepare our development environment to start developing our API.

Preparing the development environment

Before we start developing our API, we will need to prepare our development environment first. Next, we will look at the components and packages that we will be needing throughout the book.

Note

You can install the packages in any order you want. Some of the packages here are not a must-have, but it's highly recommended to install them to avoid having any issues while following the examples in this book.

Installing Node.js

To install Node.js, head to the official website, https://nodejs.org, and download the long-term support (LTS) version that matches your operating system. At the time of writing this book, version 16.13.2 is the latest LTS version and the version recommended by Strapi, as illustrated in Figure 1.2. Only LTS versions are supported by Strapi, the other versions of Node.js are not guaranteed to be compatible.

Figure 1.2: Node.js LTS version

Figure 1.2: Node.js LTS version

Once you have installed Node.js, open your favorite terminal and run the following command to verify the Node.js version:

node –v

You should see the version of the installed Node.js on the terminal.

Installing Visual Studio Code (optional)

Visual Studio Code (VS Code) is a feature-rich and powerful integrated development environment (IDE), and we will be using it as our default editor. You are free to use whichever editor you feel comfortable with. However, we highly recommend installing and using VS Code to follow along with the examples in this book.

To download and install VS Code, head to https://code.visualstudio.com and download and install the appropriate build for your operating system.

Installing Yarn

Yarn (https://yarnpkg.com) is a JavaScript package manager. We will be using it as our default package manager since it's the package manager used by Strapi itself.

To install Yarn, open your favorite terminal and execute the following command:

npm install -g yarn

Once the installation is complete, run the following command to verify that Yarn has been installed successfully:

yarn –v

The version of Yarn should be displayed in the terminal. At the time of writing this book, version 1.22 is the latest version.

Installing Docker (optional)

We will be using Docker to install and manage a Postgres database for our API in a later chapter of the book. Docker can help us easily work with different database systems such as Postgres, MySQL, or MongoDB. To install Docker, head to https://docker.com/get-started and download and install Docker Desktop for your operating system.

Once you have installed Docker, execute the following command in your terminal to verify the installation:

docker –version

The Docker version should be displayed in the terminal. At the time of writing this book, version 20.10 is the latest version.

Alternatively, you can install Postgres using a different method of your choice.

Installing Postman

Postman is a great API client, and we will use it to interact with our API. Head to the Postman website at https://www.postman.com/downloads/ and download the version matching your operating system.

Once we have the development environment set up, we can proceed with creating our API.

Creating a Strapi application

Begin by running the following command on your terminal:

yarn create strapi-app strapi-lms --quickstart 

The preceding command will set up a project using the latest version of Strapi with the default settings. Using a SQLite database, start the server on port 1337 and launch the admin dashboard.

Note

If we remove the –-quickstart flag, we will enter manual setup mode, where we will be asked a few questions to configure Strapi.

If the admin panel did not launch automatically, you can open your browser and navigate to http://localhost:1337/admin.

The first time you log in to the admin dashboard, you will be presented with a form to create the first administrator user. Complete the form to create an administrator user and sign in to the admin panel.

Overview of the admin panel

We will discuss the admin panel in greater detail in Chapter 4, An Overview of the Strapi Admin Panel. However, for now, we will just give a quick overview of the admin panel layout.

The admin panel is easy to navigate. On the left-hand side, we have our main control sidebar. It can be divided into three main categories, as follows:

Figure 1.3: Strapi main sidebar

Figure 1.3: Strapi main sidebar

Content Manager

This is where the API content lives, and you can manage API content from this section. At the moment, there is only one model, Users. We will see more models here as we progress in developing the API.

Plugins

The PLUGINS section allows you to customize Strapi. There are two essential plugins available out of the box: the Content-Type Builder and Media Library plugins.

The Content-Types Builder plugin is the core of Strapi customization; we will use it to create new models in our API and create a relationship between those models. As this plugin is critical in developing the API, we have dedicated Chapter 3, Strapi Content-Types, to discussing it in greater detail.

The Media Library plugin, as the name suggests, is used to manage all API media files.

General

This section contains Strapi settings where you can use the Marketplace to install new plugins, configure plugin settings, and edit general settings such as adding additional administrator users.

The --quickstart flag will bootstrap the Strapi application and start the development server for us. While you are developing a Strapi application, you will want to start and stop the server yourself. Let's see in the next section how to work with Strapi scripts to manage the server.

Understanding server scripts

Strapi comes with a few scripts that can be used in managing the development server as well as starting the server in production. If you open the package.json file, you will see the following four scripts:

Figure 1.4: Strapi server scripts

Figure 1.4: Strapi server scripts

The develop script

The develop script will start the server in development mode, with autoreload enabled. Basically, it will watch for any changes in the project files and restart the server if there are any. This script is intended for local development, and it should never be used in a production environment.

The start script

The start script will start the server with autoreload disabled. This script is intended to start the server in a production environment.

The build script

This script allows you to rebuild the admin panel. The Strapi admin panel is built using React.js, but in some situations, you might want to customize or extend the admin panel. In such cases, you will need to rebuild the admin panel again using the build script. The admin panel is built once when you have created a project and every time you install a plugin that requires changes to the admin panel.

The Strapi script

This script is an alias to the Strapi command-line interface (CLI), and we can use it to generate new content in our system. We will explore the Strapi CLI in the next chapter.

Summary

In this chapter, we started by explaining the concept of a headless CMS and saw how it is different from a traditional CMS. We then introduced Strapi, an open-source headless CMS, and listed the benefits of using Strapi in developing APIs.

Then, we started preparing the development environment and installed the requisite packages and software, as well as creating a sample application that we will be using throughout the rest of this book. Finally, we had a quick look at the different server scripts offered by Strapi.

In the next chapter, we will have a deeper look at the typical structure of a Strapi application. We will also build our first API in the system and learn how to use the Strapi Content-Types Builder plugin to define fields needed for the API.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Discover how Strapi can help you build APIs quickly and focus on your products and features
  • Learn how to put Strapi into practice by implementing it in real-world scenarios
  • Understand how to use Strapi's powerful features to customize your APIs

Description

Strapi is a Node.js-based, flexible, open-source headless CMS with an integrated admin panel that anyone can use and helps save API development time. APIs built with Strapi can be consumed using REST or GraphQL from any client. With this book, you'll take a hands-on approach to exploring the capabilities of the Strapi platform and creating a custom API from scratch. This book will help JavaScript developers to put their knowledge to work by guiding them through building powerful backend APIs. You'll see how to effortlessly create content structures that can be customized according to your needs, and gain insights into how to write, edit, and manage your content seamlessly with Strapi. As you progress through the chapters, you'll discover a wide range of Strapi features, as well as understand how to add complex features to the API such as user authentication, data sorting, and pagination. You'll not only learn how to find and use existing plugins from the open-source community but also build your own plugins with custom functionality with the Strapi plugin API and add them to the admin panel. Finally, you'll learn how to deploy the API to Heroku and AWS. By the end of this book, you'll be able to build powerful, scalable, and secure APIs using Strapi.

What you will learn

  • Explore Strapi and understand how it works
  • Define content types to build APIs quickly and efficiently
  • Understand authentication and authorization in Strapi
  • Create production-ready APIs with Strapi
  • Deploy the Strapi API to various environments, including Heroku and AWS
  • Use best practices to run the Strapi API in production
  • Sync permissions to access the API between multiple environments
  • Write basic tests for API utilities as well as the endpoint

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 28, 2022
Length 310 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781800560635
Vendor :
Oracle

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Feb 28, 2022
Length 310 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781800560635
Vendor :
Oracle

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together

Stars icon
Total 43.97 63.97 20.00 saved
React Projects..
€15.99 €22.99
Modern Frontend Development with Node.js
€14.99 €21.99
Designing Web APIs with Strapi
€12.99 €18.99
=
Book stack Total 43.97 63.97 20.00 saved Stars icon

Table of Contents

17 Chapters
Preface Chevron down icon Chevron up icon
1. Section 1: Understanding Strapi Chevron down icon Chevron up icon
2. Chapter 1: An Introduction to Strapi Chevron down icon Chevron up icon
3. Chapter 2: Building Our First API Chevron down icon Chevron up icon
4. Chapter 3: Strapi Content-Types Chevron down icon Chevron up icon
5. Chapter 4: An Overview of the Strapi Admin Panel Chevron down icon Chevron up icon
6. Section 2: Diving Deeper into Strapi Chevron down icon Chevron up icon
7. Chapter 5: Customizing Our API Chevron down icon Chevron up icon
8. Chapter 6: Dealing with Content Chevron down icon Chevron up icon
9. Chapter 7: Authentication and Authorization in Strapi Chevron down icon Chevron up icon
10. Chapter 8: Using and Building Plugins Chevron down icon Chevron up icon
11. Section 3: Running Strapi in Production Chevron down icon Chevron up icon
12. Chapter 9: Production-Ready Applications Chevron down icon Chevron up icon
13. Chapter 10: Deploying Strapi Chevron down icon Chevron up icon
14. Chapter 11: Testing the Strapi API Chevron down icon Chevron up icon
15. Other Books You May Enjoy Chevron down icon Chevron up icon
Appendix: Connecting a React App to Strapi Chevron down icon Chevron up icon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.