when I click on orders and then on shipments, this URL is generated:


How can I get the key for an order or shipment?



  • What actually you want to do? Apr 12, 2018 at 9:19
  • I put a button to the order overview and if it's being pressed I want it to print the package slip. Apr 12, 2018 at 9:26
  • @LKlmn please check updated answer. You can ask me if any query. Apr 12, 2018 at 9:33
  • why your want this key? Please tell us. t
    – Amit Bera
    Apr 12, 2018 at 9:45

3 Answers 3


If you simple use the method $this->getUrl() in a block that extends Magento\Catalog\Block\Adminhtml\Product\Edit\Button\Generic, the secret key will be added automatically.

Sample code for a custom button class:

namespace Vendor\Module\Block\Adminhtml\Catalog\Product\Edit\Button;

use Magento\Catalog\Block\Adminhtml\Product\Edit\Button\Generic;

class MyButton extends Generic
    public function getButtonData()

        return [
            'label' => __('Do something'),
            'on_click' => "window.location.href={$this->getUrl('my/module/url')}",
            'sort_order' => 999

Maybe there's a similar class for sales order buttons.

  • Hi, thanks for your answer. can you give me a sample for "my/module/url"? Apr 12, 2018 at 9:41
  • it depends. if you want to call a URL and do anything, you need to implement a Controller; more info about this can be found on Google or here: mageplaza.com/magento-2-module-development/… if you have problems with this, I think you should search MagentoSE or open another question Apr 12, 2018 at 9:51
$object = \Magento\Framework\App\ObjectManager::getInstance();
$url = $object->get(\Magento\Backend\Model\UrlInterface::class);
echo $url->getSecretKey();

For get key value :

echo $this->getRequest()->getParam('key');

Note : Do not use ObjectManager Direct. It defeats the purpose of dependency injection.

Your Answer

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

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