Imported Google Group message.
Date:Thursday, 5 November 2015 17:10:02 UTC.
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.