Getting Started With Strapi EP2: Collection Types
Subscribe On YouTube
In the first episode we’ve covered everything what is needed to setup Strapi and get familiar with the admin user interface. In this second episode we’re diving a little bit deeper into Strapi. We’ll setup custom Strapi collection types for the data which should be managed with strapi. We’ll also explore how to use the REST-based interface which is automatically provided.
Creating A New Collection Type
Let’s start by creating a new collection type to manage courses data. In the adminnistration panal select the link COntent-Types Builder from the left-side navigation menu. You should then see a list of already existing collection types:
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:
In input field Display name you need to enter the name of the new collection type, e.g. Course. You can then click on button Continue which will redirect you to the the view were you can start adding fields to your collection type:
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:
For The Course collection type add the following data field:
- title (of type Text)
- hours_video (of type Number)
- number_of_lectures (of type Number)
- rating (of type Number)
- url (of type Text)
The collection type view should then look like what you can see in the following screenshot:
Let’s create a second custom collection types named Author. Add the following fields to this collection type:
- first_name (of type Text)
- last_name (of type Text)
Also add a relation field to this new collection in order to create a many-to-many relation to the Course collection type:
This means that every course can have multiple authors and each author can be assigned to multiple courses.
The Author collection type should then look like the following:
Now it’s time to add some content to our collection types. Let’s start with the Author collection type by selecting the Authors entry from the left-side menu and then use the button Add New Author to add data:
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 Course collection type:
Use this form to create some Course 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 a HTTP GET request to retrieve all Course data to http://localhost:1337/courses. 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 courses 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 Course and Author collection type like you can see in the following screenshot:
The premissions we’re adding here are:
By the find permission the HTTP GET endpoint /courses is covered, so that the request now should lead to a result similar to the following:
The complete list of courses is returned in JSON format, the access is no longer forbidden.
Advertisement: Top Online Courses
The Complete Strapi Course*
Build Apps at the speed of thought with the simplest most versatile open source headless CMS
* Affiliate Link / Advertisement: This blog post contains affiliate links, which means that if you click on one of the product links and buy a product, we’ll receive a small commission. There are no additional costs for you. This helps support the channel and allows us to continue to make videos like this. Thank you for the support!