@Api\Security\MaxRequestsPerMinute

Limiting number of requests to an endpoint

This annotation allows you limit the number of request to an endpoint per minute from the current IP-address.

The basic syntax is:

@Api\Security\MaxRequestsPerMinute( $limit, $identifier )
Copied!

An example would be:

// Limit access to all endpoints with "my_id" to 10 per IP and minute
@Api\Security\MaxRequestsPerMinute( 10, "my_id" )

// Limit overall access to all endpoints using this annotation to 10 per IP and minute
@Api\Security\MaxRequestsPerMinute( 10 )
Copied!

Exceeding the given number will result in an 403 Error response.

The optional argument my_id can be any arbitrary key.

  • When using the same key in multiple endpoints, all endpoint calls with the same key will be counted
  • Without an id, all endpoints using the annotation will be counted
<?php

namespace My\Extension\Api;

use Nng\Nnrestapi\Annotations as Api;
use Nng\Nnrestapi\Api\AbstractApi;

/**
 * @Api\Endpoint()
 */
class Example extends AbstractApi
{
   /**
    * @Api\Security\MaxRequestsPerMinute(5, "getSettings")
    * @Api\Access("public")
    *
    * @return array
    */
   public function getSettingsAction() 
   {
      return ['nice'=>'result'];
   }

}
Copied!