APIs on Educative
Get introduced to how we can make APIs work.
We'll cover the following...
Working with APIs
Let’s say you want to work with APIs in your course. These APIs have various endpoints requiring various keys/tokens to function. As an author, it is easy to define stubs for these APIs in code playgrounds. Learners can get their API keys/tokens from the API’s documentation or control panel and replace them manually in the code.
Take the following playground as an example. Follow the instructions in the comments before executing the code widget below.
In order for the above playground to function, the user can replace the stubs for api_key_1 and api_key_2 manually in the code, and the API will function correctly. Similarly, the some_api_func_2 generates a token that is stored in api_key_3 and printed to the console.
The code below requires the value of api_key_3. Hence, it must be copied from the console above and used as input here:
Everything seems to work great so far. But imagine having to copy and replace the same keys for all the playgrounds across all lessons. It becomes very cumbersome for the learner to do so.
In order to make APIs more accessible, we have enabled the “API Keys” feature. Each playground can have its required API keys/tokens that are to be specified. The user can provide their values once, and this will store them globally in all the playgrounds that use those particular keys/tokens.
Educative’s API Keys
Consider, for example, the following playground with API onboarding enabled. Instead of replacing API key/token stubs in the code, we can save these in the box above the playground.
api_key_3 is automatically extracted from the code. Click “Save” to store the value in this particular key.
After you have executed the above playground successfully, you’ll see that both key values will have been replaced automatically in the playground below. You can now execute this one successfully too, and subsequently in all the lessons that have playgrounds using these keys.
So where did all the magic happen? We will explain in the following lessons.