Creating a Custom API with Strapi

June 14, 2023, . open book4 minutes read






Introduction

This post will show you how to build a custom API with Strapi, an open-source headless CMS. Strapi makes it simple to create content types, fields, and API routes by utilizing basic Node.js commands. You can easily develop a fully functional API and begin building its unique API by following this article.

What is Strapi?

Strapi is a headless CMS (Content Management System) that allows developers to build and manage content-rich websites and applications. It is a user-friendly, scalable, and adaptable platform that provides developers with a variety of features and functionalities that allow them to build powerful applications quickly and efficiently. Strapi is greatly used by developers because it’s flexible and customizable. It equally offers you the possibility to create custom APIs

What is a Custom API?

A Custom API (Application Programming Interface) is a sort of API that is designed to satisfy the special demands of a business or organization. It can be designed to carry out a certain set of tasks or to interface with a specific system.

Custom APIs, as opposed to public APIs, are intended and developed exclusively for internal usage. You can use them to streamline corporate processes, increase efficiency, and improve an organization’s performance.

With Strapi creating a custom API is just a matter of minutes.

Building a Custom API with Strapi

Prerequisites

Step 1: Install Strapi

Navigate to a desired directory and use the command below to install Strapi with rebuild data and configurations:

yarn create strapi-app my-project --template corporate

Once the command finishes running head to http://localhost:1337/admin/ via your favorite browser and fill up the form to create an administrator.

Creating a Custom API with Strapi

Step 2: Create a Content-type

Next, create a new content type called post Your post will be made up of a title, an image and content.

Creating a Custom API with Strapi

Create a Schema for the post cotent-type

Creating a Custom API with Strapi

Now that you have created the structure of your post, add one or two posts to the dashboard.

Creating a Custom API with Strapi

Note that the api folder is automatically generated with the content type you created. This content type contains three folders; controller, routes and services:

  • The Controllers are the C in the model-view-controller (MVC) design paradigm.
  • Routes handle requests delivered to Strapi on any URL. Strapi generates routes for all content types by default. The JavaScript files include a collection of methods known as actions that are accessed by the client via the desired route.
  • Services are collections of reusable functions. They are especially beneficial for adhering to the DRY programming approach and simplifying controller logic.

If you navigate to src/api/post/content-type/schema.json you will see the schema of the content-type/collection-type you created earlier

{
  "kind": "collectionType",
  "collectionName": "posts",
  "info": {
    "singularName": "post",
    "pluralName": "posts",
    "displayName": "Post"
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {},
  "attributes": {
    "title": {
      "type": "string",
      "required": true
    },
    "Feature_image": {
      "allowedTypes": [
        "images",
        "files",
        "videos",
        "audios"
      ],
      "type": "media",
      "multiple": true,
      "required": true
    },
    "Content": {
      "type": "richtext"
    }
  }
}

Globally all the content you add via the Strapi dashboard will appear in your src/api folder.

You will equally find all the uploaded images in the upload folder

Creating a Custom API with Strapi

That’s it, you have created a custom API with Strapi. If you visit localhost:1337/api/posts you will see your created posts

Creating a Custom API with Strapi

Or access the data of the n post with localhost/api/posts/n where localhost:1337/api/posts/1 for the first post

Creating a Custom API with Strapi

Now you can use your Strapi API to connect to any frontend tools like Next.js or Gatsby and pull data and populate your blog or app and deploy your Strapi application on DigitalOcean or Heruku.

Using Strapi to create a custom API is simple and straightforward. In a matter of minutes, you can have a completely usable API up and running. So, what are you holding out for? Begin developing your unique API right away!


Share on



Author: Learndevtools

Like the article? Please share or subscribe to get more updates from Learndevtools