How to create content types and fetch data through API in strapi.

How to create content types and fetch data through API in strapi.

Content Types in strapi are a collection of entered data represented by fields. Basically they are models in frameworks.

In my previous articles, I have discussed how to install strapi and create a project with it . If you are not familiar with strapi then head over to my recent articles.

Create your first Content Types in strapi

First of all, run your project using command
strapi develop

Once the project is started, navigate to http://localhost:1337/admin/. You will see something like this.

Strapi.js

Time needed: 2 minutes.

Follow these steps on how to create a content types in strpai

  1. Click on content type builder from left admin panel

  2. Click on Add Content-Type

  3. Now you have to do some Content Type’s Settings

  4. Enter the name of your article let’s say it is news

  5. Give some description like latest news

  6. Now hit Done button

Now we have created our first content type but still, we are missing the fields that make up this content type so let’s go ahead and add these fields.

News article can have these three fields.

  • title (type string)
  • content (type text)
  • image (type media)

Click on Add new field button and you will see a screen like this.

content-types

Let’s start by adding a title field, as the title should be String so click on the String button.

It will prompt a screen like this

content-types

Enter the name of the field (title) and navigate to the ADVANCED SETTINGS TAB. Here you can do some settings like you can set a default value, you can mark the field as a Required field, Unique Key, and you can also set the maximum and minimum length.

Now repeat the same process to add the remaining two fields and hit save button when done. Because of this action, strapi server will restart.

Once the three fields are added, let’s fill your content with data.

Fill your content with data

If you check Content Types on the left side of the admin panel you will notice a new content type with name News. Click on that and start adding some news in it. You will notice our three fields plus the Id. Follow these steps to fill your content with data.

  1. Click on Add New News Button to create news through admin panel
  2. Add title, Content, and image
  3. Hit save button

Ok great! Congratulations you have added your first news.

Fetch Data through API

Ok, let’s test our API, head over to browser and type http://localhost:1337/news (news is our content type). You will see a Forbidden error with a 403 status code.

content-types

We need to add some permissions to our content type news. Now click on roll and permissions from admin panel then click on Public, scroll down a bit and in Permissions section looks for News that is our content type. Click on find and on the right side you will notice a /news route.

Hit the save button.

Let’s test out this API again in the browser, and type the same URL. it’s working perfectly fine. Now we have successfully given permission to this route.

Content-types

To enhance your knowledge regarding advanced technologies, tune to Morosoft tweaks https://www.mstweaks.com/. If you want to be up to date with all that is happening in the world, Morosoft Tweaks is providing you all under one roof. We also aim to provide relevant information in a quick and sophisticated manner. Contact us: info@morosoft.pk

Share this post

Post Comment