How to create custom Rest API end point for creating node with file upload

Here we are going to discuss about how to create a custom Rest API resource which programmatically create a node.

There are in built restful web service available in Drupal 8 and Drupal 9. You can read full article here.

But some cases suppose if you want to apply some custom business logic on node creations , you need create your own Rest resources which will create node with fields programmatically.

First enable Restful web service module in Core module list, Also download and install Rest UI module.

Here we are using below sample code ,

Drupal 8 –

Download above module and install it.

Under Configuration -> Web services

Click on Rest.

Enable below resources.

Also configure Authentication methods and other configurations.

Now you have a rest API end point where you can post data with parameters


Here our sample module have below folder structure.

Here we are going to write our node creation code in RestSamplepost.php

Provide below code in post function , this will create an article content.

This will create a article content with title , body content and image.

Here you can see we are receiving fid for image file.

SO fist we need to upload image using Rest API already available in Restfull webservice.

Here we have a basic html file outside Drupal project. See below html form .

First we need to upload file and get fid.

Place below code inside script tag of same html file.

For generating X-CRF token , refer this article.

SO here click on brows button , on select image, file will be uploaded to the server and , receive details like fid , so in tag <span id=”fidText”> </span> you can see uploaded files f_id

In our RestSamplepost.php, you can see our custom Rest API URL is /rest/digitalnadeem/api/post/items

You can get this from below section

“create” = “/rest/digitalnadeem/api/post/items”

Provide below code in form submit handler.

SO here I have placed my html fie inside Drupal project folder


See the complete code of this html file.

Replace with your project name.

So you can run the html fie as below.

After submission you can see newly created article page with title, body and image fields in /admin/content page.

Download sample module digital_nadeem and html file here.


Get Free E-book
Get a free Ebook on Drupal 8 -theme tutorial
I agree to have my personal information transfered to MailChimp ( more information )
if you like this article Buy me a Coffee this will be an ispiration for me to write articles like this.

You may also like...