Tyk v2.2 Documentation Components

HomeTyk v2.2 Documentation ComponentsTutorial: Create a security policy using the API

Tutorial: Create a security policy using the API

Security Policies can be created with a single call to the API. It is very similar to the token creation process. To generate a simple security policy using the Tyk Cloud API you can use the following curl command:

curl -X POST -H "authorization: {API-TOKEN}"
 -s
 -H "Content-Type: application/json"
 -X POST
 -d '{
      "access_rights": {
        "{API-ID}": {
          "allowed_urls": [],
          "api_id": "{API-ID}",
          "api_name": "{API-NAME}",
          "versions": [
            "Default"
          ]
        }
      },
      "active": true,
      "name": "POLICY NAME",
      "rate": 100,
      "per": 1,
      "quota_max": 10000,
      "quota_renewal_rate": 3600,
      "tags": ["Startup Users"]
    }'
 https://admin.cloud.tyk.io/api/portal/policies | python -mjson.tool

You must replace:

  • {API-TOKEN}: Your API Token for the dashboard API.
  • {API-ID}: The API ID you wish this policy to grant access to, there can be more than one of these entries.
  • {API-NAME}: The name of the API that is being granted access to (this is not required, but helps when debugging or auditing).
  • POLICY NAME: The name of this security policy.

The main elements that are important are:

  • access_rights: A list of objects representing which APIs that you have configured to grant access to.
  • rate and per: The number of requests to allow per period.
  • quota_max: The maximum number of allowed requests over a quota period.
  • quota_renewal_rate: how often the quota resets, in seconds. In this case we have set it to renew every hour.

When you send this request, you should see the following reply with your Policy ID:

{
    "Message": "577a8589428a6b0001000017",
    "Meta": null,
    "Status": "OK"
}

You can then use this policy ID in the apply_policy_id field of an API token. Please see the relevant documentation on session objects for more information about how tokens are attached to policies.

For more information on how policies are constructed and a detailed explanation of their properties, please see the Security Policies section.

Tags
Was this article helpful to you? Yes No