Drupal : Building Custom API with RESTful Views in a Flash!


First, let me ask you. How does your favorite social app let you create and login with your google credentials? Or How does your weather app get today’s weather forecast? - the answer to these questions is that they communicate with other systems to get the data or verify your credentials. The way these two systems communicate together is to do something called an API.

API is an acronym, and it stands for “Application Programming Interface.” An API simply states the rules for the communication to happen. The main process is retrieving data from an endpoint and displaying in a format depending on whatever you want. 

In Drupal, API’s can be created and modified through the default core module RESTful Web Services and Views. This module exposes entities and other resources as RESTful web API and with the help of Serialization module we could choose different formats depending on our preferences.

With everything introduced let’s go on building our own API.

1. In the Drupal Extend (/admin/modules) page, under the web services, we’ll enable the RESTful web API and Serialization module.

1
 

2. We’ll go to Views (/admin/structure/views), and create a new view configuration namely “My Custom API” and choose the type of contents/taxonomy you want to render.

2

3. Still in the Add View Page, make sure to check the “Provide a REST export” checkbox under REST export settings. After the checkbox is ticked it will show another input field which is “REST export path” this field would determine the url path for our Views custom API. For this example we’ll just have it “my_custom_api” then hit “Save and Edit”.

3


4. After saving, we’ll edit the “Settings” beside “Serializer” under “Format” then will choose “json” for this example.

4

5

Note : We could select other formats depending on our needs.

5. Next part is what we want to display, still under the “Format” section we’ll click the “Entity” and select “Fields” for this example then click “Apply”.

6

Note : We could select other displays depending on our needs.

6. The default fields to be shown after selecting “Fields” is “Title”. For this example we’ll remove the link to node in the title by unchecking the “Link to the Content” checkbox then hit “Apply”.

7


7. Hang on! We’re almost done. After everything is configured it’s up to you if you want to add more fields to be displayed. For this example we’ll just show every “Title” in the content that we selected while we were adding the View earlier. Your view configuration should look like this.

7
 

8. The last part is testing the custom API that we made using the view configuration that we made above. To test this, simply visit the path that we specified under the “REST export path” earlier which is “my_custom_api”, the url path should look like this [scheme://host]/my_custom_api.

The end result should look like this, a JSON generated value from our custom API.

8

Conclusion :

With RESTful Views we could create and modify API’s with specific formats easily in a flash.

 

Profile picture for user Jessie Allen Anosa
Jessie Allen Anosa Front-End Specialist

"Kumusta!". I call myself Jessie! A Filipino nomad raised in Philippines. Currently a Front-End Developer here in Acret-PH which is an awesome job that highlights my passion in designing and making things look good. Working here taught me a lot with the challenges and experience new things which helped me grow and continue to thrive to be better and do better, together with the support of the team and management it drives me to push myself to my limits and break my boundaries to be one of the person who will you can count and rely on. To sum it all, I'm in my happiest designing and making my living most from it :) — and trust me, I know things.