I'm creating an extension that exposes an API to another web site. My extension will create an API key, and then you'll copy this API key into the other web site, which will let Magento and the other site exchange data.

I've created a system.xml entry for my API key. I'd like for the default value for this config item to be populated by a call to a generateApiKey method defined in PHP code.

In pseudocode, config.xml:

                <apikey><?php echo $obj->generateApiKey(); ?></apikey>

I know that that's not possible, but hopefully that conveys what I want to do.

How can I programmatically generate (and save) a default value for something defined in system.xml?

2 Answers 2


I'd suggest populating the value via a data migration script. Create a resource in your module's config.xml:


Create a the migration script: MyNamespace/MyExtension/data/my_namespace_my_extension/data-install-1.0.0.php

/* @var $this Mage_Core_Model_Resource_Setup */
$generatedKey = Mage::helper('my_extension')->generateApiKey();
$this->setConfigData('my_extension/api_connection/apikey', $generatedKey);

NOTE: the cache would most likely need to be cleared after installation. This should be made clear in your module's installation instructions. It should also be possible to flag the cache as invalidated.

  • Is that going to work? The setConfigData method will set the node value, but it won't persist the value to a database anywhere. Mar 10, 2014 at 18:59
  • 1
    It should, checkout the source here. Are you referring to Mage::getConfig()->setNode() perhaps?
    – beeplogic
    Mar 10, 2014 at 19:02
  • Should this install script have the usual $installer->startSetup() and $installer->endSetup() calls?
    – skyler
    Mar 10, 2014 at 19:11
  • It's not necessary, the "data" scripts are meant for populating data. The start/end setup methods concern themselves with how the database behaves (e.g. turning off foreign key constraints).
    – beeplogic
    Mar 10, 2014 at 19:19

You can just save it into core_config_data.

So either in $setup->setConfigData($path, $value) or Mage::app()->getConfig()->saveConfig($path, $value) but be careful, the second one doesn't clean the cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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