HomeBlogsBackend development

Getting started with strapi - Part II

An exploration of the Strapi framework and features offered

"# Collection Type

Creating a new collection type

Let’s start by creating a new collection type to manage book data. In the administration panel select the link Content-Types Builder from the left-side navigation menu. You should then see a list of already existing collection types:

strapi-content-manager Click on the link Create new collection type to start creating a new one. You should then be able to see the Create a collection type form:

02-strapi-create-content-type In input field Display name you need to enter the name of the new collection type, e.g. Book. You can then click on the button Continue which will redirect you to the the view were you can start adding fields to your collection type:

03-strapi-new-type.png Here you can select from a list of available data types to add new fields to the collection. For the first field to add select data type Text and then specify the name of the new field in the next screen: 04-strapi-base-settings.png

You can also click on the advanced settings tab on top-right to add some validation rules as seen below 05-strapi-advanced-settings.png

For the Book collection type add the following data field:

  • title (of type Text)
  • description (of type Text)
  • rating (of type Number)

The collection type view should then look like what you can see in the following screenshot: 06-strapi-book-view.png Click on the Save button. This will create the collection type and will also restart the server with the new collection type.

Creating Content

When adding new data to a collection type Strapi providers you with a data entry form which includes the fields which have been added to the collection type. In the following screenshot you can see the form which is provided if you create new data for the Book collection type:

Use this form to create some Book data sets as well.

Now that we’ve added data into our collection types we’re ready to test the REST API which is automatically provided for the newly added collection types. E.g. we’re able to initiate an HTTP GET request to retrieve all Book data to http://localhost:1337/books. The result of this request is then displayed in the browser:

Ok, this is not what we’ve expected. The HTTP response code is 403 which means that accessing this endpoint is forbidden. No books data is returned. So how can we solve this authorization problem? Let’s go back to Strapi’s administration panel and open up the Roles & Permissions plugin:

Here you can see that two roles are defined by default:

Authenticated: This is the role which is given to authenticated users by default Public: This is the role which is given to unauthenticated users by default The HTTP GET request we’ve just initiated as done in an unauthenticated way so that the Public role is applied here. Click on the Public role to the see role definition in detail.

In the Permission section, we’re now able to add specific permissions for the Book collection type like you can see in the following screenshot:

The permissions we’re adding here are:

count find findOne By the find permission, the HTTP GET endpoint /books is covered so that the request now should lead to a result similar to the following:

The complete list of books is returned in JSON format, the access is no longer forbidden."

Made with ❤️ by me. Thanks to these .