When adding APIs using the Advanced Management API I've come across a small problem.

In one of our use cases we add an API and then a key for that API. I've noticed when we get to about 16 APIs associated with the adding of a key to an API failes with an error: "Failed to save new session object to Tyk: Could not create key for this API ID, API doesn't exist.". However, I have checked the APIs and the API we're adding does exist.

Having done some testing I've noticed the following:

add api

get that apis internal id by doing a get api

add a second api

get the second apis internal id

add key for api

add key for second api (fails)

However, if we move the add of the key to above the add of the second api we get:

Is the API Keyless? Keyless APIs might cause this issue because they have no storage back-end.

Alternatively, what's the time difference between adding the API and adding the key? The API, once added, is saved to the DB, and then the Tyk nodes must pick up and load that API after they get the reset command from the bus, only once that happens can you create keys, so if it happens very fast, you may be outrunning the reload logic. Try adding a timer before creating the token.

Also, strictly speaking, you do not need the API ID for each API to create keys, you only need the first one, unless you've created your own storage back ends, so try the above again but use the first API ID you create throughout your key adding function.

Is the API Keyless? Keyless APIs might cause this issue because they have no storage back-end.

Alternatively, what's the time difference between adding the API and adding the key? The API, once added, is saved to the DB, and then the Tyk nodes must pick up and load that API after they get the reset command from the bus, only once that happens can you create keys, so if it happens very fast, you may be outrunning the reload logic. Try adding a timer before creating the token.

Also, strictly speaking, you do not need the API ID for each API to create keys, you only need the first one, unless you've created your own storage back ends, so try the above again but use the first API ID you create throughout your key adding function.