Designing Web APIs with Strapi

Book description

Leverage the power of Strapi to build self-hosted, customizable, and performant content APIs

Key Features

  • 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

Book 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

Who this book is for

This book is for backend and frontend JavaScript developers. Experienced API developers will learn a new, fast, and flexible way of building APIs, while frontend developers will be able to take a step toward becoming full-stack developers by learning how to leverage Strapi for building APIs quickly. Basic knowledge of JavaScript and REST API concepts is assumed.

Table of contents

  1. Designing Web APIs with Strapi
  2. Contributors
  3. About the authors
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
      1. Using this repository
      2. Following along with the book
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share your thoughts
  6. Section 1: Understanding Strapi
  7. Chapter 1: An Introduction to Strapi
    1. What is Strapi?
    2. Why use Strapi? (The benefits of Strapi)
      1. Open-source Node.js
      2. Database-agnostic
      3. Customizable (extendable)
      4. RESTful and GraphQL
      5. Users and permissions
    3. Preparing the development environment
      1. Installing Node.js
      2. Installing Visual Studio Code (optional)
      3. Installing Yarn
      4. Installing Docker (optional)
      5. Installing Postman
    4. Creating a Strapi application
      1. Overview of the admin panel
    5. Understanding server scripts
      1. The develop script
      2. The start script
      3. The build script
      4. The Strapi script
    6. Summary
  8. Chapter 2: Building Our First API
    1. Exploring the project structure of a Strapi application
      1. The commands to run Strapi
      2. The project structure
      3. Under the hood – the components that make up Strapi
    2. Defining the API requirements
    3. Creating our first content-type – a classroom
      1. Using the Strapi CLI instead of the admin panel
    4. Clarifying Strapi terminology
      1. Content-types, resources, object types, and models
    5. Interacting with the Strapi API
      1. Setting permissions
      2. What did we just create?
    6. Understanding the makeup of a Strapi API
      1. Routes, controllers, services, and models
      2. Content-types in code
    7. Summary
  9. Chapter 3: Strapi Content-Types
    1. What is the Content-Type Builder plugin?
    2. Creating and managing content-types
      1. Creating a title field
      2. Creating a slug field
      3. Creating a type field
      4. Creating url and contents fields
    3. Understanding relations in Strapi
      1. One-way
      2. One-to-one
      3. One-to-many
      4. Many-to-one
      5. Many-to-many
      6. Many-way
      7. Creating a Tutorial and Classroom relation
      8. Creating a tutorial from the admin panel
    4. Interacting with the API endpoints from Postman
    5. Differences between SINGLE TYPES and COMPONENTS
      1. Creating components
    6. Summary
  10. Chapter 4: An Overview of the Strapi Admin Panel
    1. Navigating around the admin panel
      1. Content Manager
      2. PLUGINS
      3. GENERAL
      4. Marketplace
      5. Plugins
      6. Settings
    2. Managing admin users
      1. The difference between Users and admin users
      2. Creating new admin panel users
      3. Managing admin panel roles
    3. Managing content effectively
      1. Searching and filtering to query our data
      2. Customizing the table view
      3. Customizing the details view
    4. Using the Media Library
      1. Media types in API responses
    5. Summary
  11. Section 2: Diving Deeper into Strapi
  12. Chapter 5: Customizing Our API
    1. The routes – where it all starts
      1. The default routes
      2. Disabling a core route
      3. Adding a new route
    2. Handling routes with controllers
      1. Adding a new controller handler for our endpoint
      2. Controllers – it's all about context
    3. Reusing logic with Strapi services
      1. Communicating with the database using the Entity Service API
      2. Returning a unified response
      3. Populating relationships
      4. Sanitizing data – hiding passwords
      5. Overriding the default CRUD APIs
    4. Tweaking database queries and responses with life cycle hooks
      1. Using hooks
      2. DRYing the hook's logic with services
    5. Summary
  13. Chapter 6: Dealing with Content
    1. Creating sample data
      1. Cleaning up
    2. Sorting API content
      1. What about sorting on multiple fields?
    3. Filtering API content
    4. Paginating API content
    5. Under the hood – how it works
    6. Summary
  14. Chapter 7: Authentication and Authorization in Strapi
    1. Understanding the difference between admin users and API users
    2. API login and sign-up
      1. What if I want to change the JWT expiration time?
      2. What if I want to change the JWT secret?
    3. Securing API routes
      1. Under the hood – how it works
    4. Using Strapi roles and permissions
      1. Creating the Student role
      2. Creating the Teacher role
      3. Creating the Admin role
      4. What if I want to change the default role for a new user?
    5. Working with policies
      1. Creating a policy
    6. Honorable mention – OAuth providers
    7. Summary
  15. Chapter 8: Using and Building Plugins
    1. Exploring the Strapi plugin ecosystem
      1. What is a plugin in Strapi?
    2. Installing and using plugins from the Marketplace
      1. Installing the API Documentation plugin from the CLI
      2. Finding more plugins
    3. Use case – Enabling GraphQL for our API
    4. Use case – Sending an email from Strapi
      1. Sending an email using the Email plugin
      2. Plugins and providers
      3. Hooking our API to send an email
    5. Creating our own plugins
      1. Plugin development – frontend versus development
      2. Building our plugin
      3. Updating the model
      4. Building the Enrollments component
      5. Creating an API token
      6. Displaying enrollments in the plugin
      7. Using Strapi Design System
    6. Summary
  16. Section 3: Running Strapi in Production
  17. Chapter 9: Production-Ready Applications
    1. Seeding the database
    2. Keeping permissions in sync
    3. Serving media from an S3 bucket
    4. Using PostgreSQL
      1. Testing PostgreSQL locally
    5. Summary
  18. Chapter 10: Deploying Strapi
    1. Deploying to Heroku
    2. Deploying to AWS Fargate
      1. Step 1 – Creating a Docker image for our API
      2. Step 2 – Creating a Docker repository on AWS ECR
      3. Step 3 – Creating an AWS Fargate cluster
      4. Troubleshooting
      5. Cleaning up
    3. Summary
  19. Chapter 11: Testing the Strapi API
    1. An overview of software testing
    2. Configuring test tools
    3. Setting up the test environment
      1. Preparing the database configuration
      2. Preparing the Strapi instance
    4. Writing and running tests
      1. Testing a public endpoint
      2. Testing a secure endpoint
    5. Summary
  20. Appendix: Connecting a React App to Strapi
    1. About the React app
    2. Understanding the React app folder structure
    3. Running the app
    4. Why subscribe?
  21. Other Books You May Enjoy
    1. Packt is searching for authors like you

Product information

  • Title: Designing Web APIs with Strapi
  • Author(s): Khalid Elshafie, Mozafar Haider
  • Release date: February 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781800560635