# Set or change API key and update it in DB

> Add a server script behind urlSetApiKey that receives the n1edApiKey the configuration widget posts and saves it in your database.

Source: https://n1ed.com/docs/custom-cms/set-and-change-api-key  
Section: For developers > Custom CMS > Set and change API key  
Updated: 2026-09-29  
Every page of n1ed.com has a Markdown copy at its URL + `.md` (`index.md` after a URL that ends in `/`); all of them are in one file: https://n1ed.com/llms-full.txt

The [configuration widget](https://n1ed.com/docs/custom-cms/configuration-widget) of the next step lets an administrator set or change the API key from your admin panel. It does not write to your database itself: it posts the new key to a URL of your server, and a script of yours saves it. This page is that script.

The widget sends a `POST` request with two form fields:

- `n1edApiKey`: the new API key, 24 characters. The widget has already checked it with the N1ED cloud before posting.
- `n1edToken`: always `-`. Older versions of the widget also stored an access token here; the field stays for compatibility, and your script can ignore it.

Create the script (say `setApiKey.php`) and let it save the key where the [previous step](https://n1ed.com/docs/custom-cms/store-api-key) reads it. PHP sample:

```php
<?php

    // Do not forget to check that the user has administrator permissions here

    $apiKey = $_POST["n1edApiKey"];

    if ($apiKey == NULL || strlen($apiKey) != 24) {
        throw new AccessDeniedHttpException();
    }

    // Replace it with the tools your CMS offers for interacting with the DB
    mysql_query(/* some query saving the API key into the DB */);

?>
```

Any response with a 2xx status code counts as success; on any other the widget shows an error with the response. If your admin panel protects POST requests with a CSRF token, the widget can send it: see `urlSetApiKey__CSRF` on the next page.

#### Check it

There is nothing to see yet: the script is called by the widget the next step adds. To test it alone, post `n1edApiKey=<one of your keys>` to it with `curl` and check the database.
