Creating custom Developer Portal Keys

When importing custom keys into Tyk, I can certainly do that via API.
However, how would I import a custom key that’s tied to a specific Developer on the Developer Portal?

This is possible via APIs. We need to import the custom key and then update the Developer object to manually attach the custom key to the Developer.

  1. Create your custom key:
curl --location 'DASH_HOST/api/keys/my_custom_key' \
--header 'authorization: <dash_api_key>' \
--header 'Content-Type: application/json' \
--data '{
    "quota_max": -1,
    "allowance": 0,
    "rate": 0,
    "per": 0,
    "tags": [
        "alias-my-custom-key"
    ],
    "org_id": "5e9d9544a1dcd60001d0ed20",
    "alias": "alias-my-custom-key",
    "apply_policies": ["5f83cfd378ab040001d3d824"]
}   '

It’s now imported in the Tyk Dashboard, we will need this information later, such as key hash and attached Policy ID.
image

  1. Retrieve the Developer object:
curl --location 'DASH_HOST/api/portal/developers/<dev-id>' \
--header 'Authorization: <dash_api_key>'

Response:

{
    "id": "646cb871b0023900017e615b",
    "email": "[email protected]",
    "date_created": "2023-05-23T12:58:25.689Z",
    "inactive": false,
    "org_id": "5e9d9544a1dcd60001d0ed20",
    "keys": {
        "48ea222a8d184958": [
            "5ead7120575961000181867e"
        ]
    },
    "subscriptions": {
        "5ead7120575961000181867e": "48ea222a8d184958"
    },
    "fields": {},
    "nonce": "",
    "sso_key": "",
    "password_max_days": 0,
    "password_updated": "2023-05-23T12:58:25.762Z",
    "last_login_date": "2023-05-23T13:06:33.56Z"
}

Then, we have to modify the subscriptions field and the keys field by adding a map to the key_hash and policy_id.

in keys, the field name is the key hash, or this value:
image

so here, 7ce... becomes the key, and the attached policy ID is the value of that key.

And then we do the inverse in the subscriptions object. Here’s the final object to send back to Dashboard:

curl --location --request PUT 'DASH_HOST:3000/api/portal/developers/<developer_id>' \
--header 'Authorization: DASH_API_KEY \
--header 'Content-Type: application/json' \
--data-raw '{
    ...
    "keys": {
        "48ea222a8d184958": [
            "5ead7120575961000181867e"
        ],
        "7cef29dc61dff20f": [
            "5f83cfd378ab040001d3d824"
        ]
    },
    "subscriptions": {
        "5ead7120575961000181867e": "48ea222a8d184958", 
        "5f83cfd378ab040001d3d824": "7cef29dc61dff20f"
    },
    "...
}'