I created a rest service which every user on the site calls from a product we are selling.

How do I count the number of calls for every user so I can generate a report of this.


2 Answers 2


Do your REST service needs authentication?

If yes, you just log each access attempt by users: One attempt per one record per user or one record per user and updating the count column always.

If there is no authentication, you will have harder time to achieve this. It would be also good to know HOW they are making this rest calls (eg: clicking on a button which resolves into a rest call?).

You could still try to check session and find out which user made the requeset.

  • The rest service is called from by a VBA script in a excel sheet which we make as well. There is no authentication (besided the fact how to arrange that :-) but if that's part of the solution we will created that. The script is calling this url, we return josn data or [] that's it.
    – Justme
    Mar 21, 2018 at 17:47
  • Well if you have no authentication, and the rest call is also no made from on the webpage, where you could have had the user session, currently there is no way to determine who made the request!
    – ssibal
    Mar 22, 2018 at 8:53
  • Ok how to add authenciation and to count then?
    – Justme
    Mar 22, 2018 at 12:32

You can create a new Content Entity. With this entity you can save all calls on the rest service (p.e. user, url or product, time, ...).

Drupal creates a database table for each entity and with the ContentEntityBase it is easy to save new entries, read all entries, read part of the entries, search or whatever you want to do.

There exists a big introduction & documentation


When you save all the calls you can easy read the calls out with the entity. And based on the informations you have saved, you can count the number of calls to your rest service and you can generate the report.

Code Examples

  1. Create the module-folder example_module in modules/custom
  2. Add the info-file:



type: module
core: 8.x
  1. Add the Class to create your entity


namespace Drupal\example_module\Entity;

use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;

 * Defines the Example Module entity.
 * @ingroup example_module
 * @ContentEntityType(
 *   id = "example_entity_name",
 *   label = @Translation("Example entity"),
 *   base_table = "example_entity_name",
 *   entity_keys = {
 *     "id" = "id",
 *     "user_id" = "user_id",
 *   }
 * )
class ExampleEntity extends ContentEntityBase {

   * {@inheritdoc}
   * Define the base fields.
  public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

    // Standard field, used as unique if primary index.
    $fields['id'] = BaseFieldDefinition::create('integer')
      ->setDescription(t('The ID of the entity.'))

    // Save user_id as a string
    $fields['user_id'] = BaseFieldDefinition::create('string')
      ->setDescription(t('The associated user.'))
        'max_length' => 255,
        'text_processing' => 0,

    // You can replace the fields or add more. Feel free!

    return $fields;
  1. Enable the module: $ drush en example_module

  2. Use the entity to save or load content. For example in a Controller of the rest service.

// Somewhere above the Controller
use Drupal\example_module\Entity\ExampleEntity;

// Create a new entry
$user_id = Drupal::request()->get('user');
$entityContent = array(
  'user_id' => $user_id,
$exampleEntity = ExampleEntity::create($entityContent);

// Load entries by user_id
$entityTypeManager = Drupal::entityTypeManager();
$exampleEntityStorage = $entityTypeManager->getStorage('example_entity_name');
$entries = $exampleEntityStorage->loadByProperties($entityContent);
// Now you can count the entries.

Be careful, there are different ways to work with Drupal and different ways where to place your own code. Without seeing your code base I can only help with this "default way".

  • You didn't mention anything about the solution. I don't know how is your answer relevant in any way.
    – ssibal
    Mar 21, 2018 at 14:37
  • If I misunderstood the question (How to count the number of calls to a rest service?), sorry! I understood that @Justme wants to count the number of calls. So probably the solution could be to save each call (with a new Content Entity) and after that you can count them by reading out of the new Entity. Probably the question is not really clear, I see that we (@ssibal and me) have different understandings about it's content.
    – btemperli
    Mar 21, 2018 at 16:01
  • @btemperli, I don't know if you misunderstand me. The only thing I see is that is for me not clear how to implement your solution. I understand the idea but how to do it is for me not clear. Sorry for this.
    – Justme
    Mar 21, 2018 at 17:46
  • @Justme I added now an example where you can see how to do it. Does this helps for you?
    – btemperli
    Mar 21, 2018 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.