Skip to content

Core\Config::secret

Returns one value from the [secrets] block of nvs.toml as a secret string, or null if no block sets it.

Use it for a password, an API key or a token. A secret can go into an outbound HTTP header, a form field, a JSON body and a database password. echo, print, Core\Log::write and a response body do not accept it. A program that tries does not compile.

api_key = "…" writes the value in nvs.toml. api_key_file = "api_key.txt" reads it from a file. A relative path starts at the folder of nvs.toml. One newline at the end of the file is removed.

In plain words: a secret is a value your program can use but never show. The name must be text your program wrote. A name from a request does not compile, so a visitor cannot choose the secret.

Good to know: [app.secrets] under [[app]] replaces the [secrets] value with the same name. Core\Config::get and Core\Config::all never return a secret. nvs prints a warning when other accounts can read a secret file, so make the file readable only by the account that runs nvs.

Parameters

  • $namestringThe name of the secret, such as api_key. The block may write the value, or read it from a file with api_key_file. The name cannot come from the request.

Return value

Returns ?secret string. The secret as a ?secret string. Your program cannot print it or write it to a log. You can send it in an outgoing header, a form field or a database parameter. The result is null when no block sets the name.

Examples

The examples below run with this configuration file.

nvs.toml
# The secrets these examples read. These values are not real.
[secrets]
api_key = "example-api-key"
payment_token = "example-payment-token"

Send an api key in a header

<?nvs
// Shows how to read an API key from `nvs.toml` and send it in a request header.
// The server's reply is fixed here, so the example runs without a network.
Core\Test::answerHttp('https://api.example.com/orders', 200, { body: '3 open orders' });
// `Core\Config::secret` returns a `secret string`. The `[secrets]` block of
// `nvs.toml` sets `api_key`, so `$key` has that value.
secret string $key = Core\Config::secret('api_key') ?? '';
// A request header accepts a secret. This prints `200 3 open orders`.
var $response = Core\Http\Client::get('https://api.example.com/orders', { headers: ['authorization' => 'Bearer ' . $key] });
echo $response->status(), ' ', $response->text(), "\n";
Output
200 3 open orders

Send a token in a json body

<?nvs
// Shows how to send a secret token to another server in a JSON body.
// The server's reply is fixed here, so the example runs without a network.
Core\Test::answerHttp('https://pay.example.com/refunds', 201, { body: 'refund created' });
// The `[secrets]` block of `nvs.toml` sets `payment_token`.
secret string $token = Core\Config::secret('payment_token') ?? '';
// A JSON body accepts a secret. This prints `201 refund created`.
var $response = Core\Http\Client::post('https://pay.example.com/refunds', { json: ['token' => $token, 'order' => 381] });
echo $response->status(), ' ', $response->text(), "\n";
Output
201 refund created

Stop when a secret is missing

<?nvs
// Shows how a program stops with a clear message when a secret it needs is not set.
class Secrets
{
// Returns the secret `$name`, or throws an error when no block sets it.
public static function required(string $name): secret string
{
var $value = Core\Config::secret($name);
if ($value == null) {
throw new RuntimeError("the secret {$name} is not set in nvs.toml");
}
return $value;
}
}
// `api_key` is set, so this prints `the shop has its API key`.
secret string $key = Secrets::required('api_key');
echo "the shop has its API key\n";
// `mail_password` is not set, so this prints the error message.
// The message has the name of the secret. It never has the value.
try {
secret string $mail = Secrets::required('mail_password');
echo "the shop can send mail\n";
} catch (RuntimeError $error) {
echo 'The shop cannot start: ', $error->message, "\n";
}
Output
the shop has its API key
The shop cannot start: the secret mail_password is not set in nvs.toml