Using API Keys for Extractions

Learn how to extract keys from the output of a code.

We'll cover the following

Extracted API keys

We can specify the keys/tokens that need to be extracted from the console output of a coding playground:

Once we have specified them in the widget, we need to use “Output Transform” (as explained in the previous lesson) to specify what part of output goes against its respective key/token value. Here is the sample stub for output transform code:

function outputTransform(stdout, stderr) {
  const apiKeyValList = stdout.split(' ');
  const apiKeys = {
    api_key_2: apiKeyValList[0], 
    api_key_3: apiKeyValList[1],
  };
  return { apiKeys, stdout, stderr };
}

Make sure that the variable names match the names of the API keys. Now, when you press “Run”, the key extraction window will pop up at the bottom of the widget. This window will display all the key-value pairs being extracted from the playground:

After saving the extraction, the value for a particular key will be updated across all lessons!

Let’s see how this works by executing the code in the playground below.

#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
}

The new values of the relevant API keys are updated:

#include <iostream>
using namespace std;
int main() {
cout << "{{api_key_2}}" << endl;
cout << "{{api_key_3}}" << endl;
}

These key/token values are globally saved for all lessons and will be available to use in any playground that subscribes to them as required keys as was discussed in Using API Keys for Replacements.