I'm trying to save / get encrypted data in my core_config_data table, such as an API key for example. In my system.xml file I have it set to obscure:

<field id="my_access_token" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label" type="obscure">
    <label>Access Token</label>

And then in my config.xml file I have:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
        <my_access_token backend_model="Magento\Config\Model\Config\Backend\Encrypted" />

This allows me to to retrieve / decrypt the value when using:

$this->scopeConfig->getValue('test/api/my_access_token', $storeScope);

However, if I want to save a new value with the following, it saves as plain text / unencrypted in the core_config_data table

$this->configInterface->saveConfig('test/api/my_access_token', $myAccessTokenValue, 'default', 0);

1 Answer 1


Just came across my answer thanks to this post: https://magento.stackexchange.com/a/126874/35364

Basically just needed to inject:

protected $encryptor;

public function __construct(
    \Magento\Framework\Encryption\EncryptorInterface $encryptor,
    $this->encryptor = $encryptor;

And then save my data with the following:

$this->configInterface->saveConfig('test/api/my_access_token', $this->encryptor->encrypt($myAccessTokenValue), 'default', 0);

Not the answer you're looking for? Browse other questions tagged or ask your own question.