Using API Keys for Replacement

Take a demo of the key replacement feature.

Required API leys

Each playground can subscribe to a subset of the global API keys as follows:

Click "Select API Keys" button
Click "Select API Keys" button

The author may select the required ones from the global keys. Suppose, api_key_1 and api_key_2 are selected as the “Required” keys in a particular playground as follows:

This playground will now detect these API keys in the code when it is executed. These keys will be detected based on the following patterns in code:

  • ;;;key;;;
  • <<key>>
  • ==key==
  • {{key}}

The patterns above must be used in the code as strings, i.e., they must be enclosed in quotation marks.

If a key is being used in the playground, but its value has not been provided, an error will be thrown.

Detecting keys in code playgrounds

The following image illustrates how the patterns listed above can be used to detect the required keys in the code:

Editor view
Editor view

Below is the running example of how the learner views it:

#include <iostream>
using namespace std;
int main() {
// your code goes here
cout << "{{api_key_1}}" << endl;
cout << "<<api_key_2>>" << endl;
cout << "==api_key_1==" << endl;
cout << ";;;api_key_2;;;" << endl;
}

We may try executing the code first without saving the keys, and then after saving the keys to see what it looks like to learners. Once the learner saves the keys, they will be replaced in the code matching the above patterns in all the playgrounds across the course. They can be modified by the learner from any of the playground subscribing to those keys using the API keys box above the playground, and they will be replaced globally for that user in all the lessons.

Let’s try modifying the keys in the box below and see how it works:

#include <iostream>
using namespace std;
int main() {
// your code goes here
cout << "{{api_key_1}}" << endl;
cout << "{{api_key_2}}" << endl;
return 0;
}

After the key values are modified from the box above the playground, they can be seen modified in the previous playground too.