Skip to main content

Storing API key in DB and injecting it into N1ED

N1ED can be installed on many websites, and each of them needs an API key of its own: the key selects the configuration the editor loads, its custom blocks and its file storage. A CMS you ship to many customers therefore injects the API key into N1ED dynamically, and lets an administrator set or change it without touching code. This page does the first half: the key comes from your database.

Create a field for the API key in the database (a string of 24 characters). Its default value is the shared demo key of N1ED, N1ED24RR1234123412341234: N1ED runs on it out of the box, with a shared configuration nobody can change, until an administrator sets a key of their own in the next steps.

Then read the key from the database and pass it to the editor where you initialize it, as apiKey. For TinyMCE and PHP:

<?php
$apiKey = getN1EDApiKeyFromDB(); // write your own function
?>
<script>
tinymce.init({
selector: "#editor",
plugins: "n1ed",
apiKey: "<?php echo $apiKey ?>"
});
</script>

This only reads the key and passes it as it is; the next step adds the script that updates it.

Check it​

Be sure N1ED loads with your default API key. Its configuration is the one the Dashboard shows for that key: on the shared demo key it cannot be changed, which is what the next steps are for.