restshopwareshopware6shopware6-api

Access to Shopware API from CLI command without Client Credentials


I would like to call some Shopware API endpoint from CLI command (user is not logged in).

I am able to create Client Credentials and use it for my calls - but it requires to generate Client Credentials manually by user in "Settings > System > Integrations".

creating client credentials

Is it possible to avoid generating Client Credentials and then copy/paste to some config?

My only idea is to generate Client Credentials directly in plugin code and store them in DB as a plugin config for example:

use Shopware\Core\System\SystemConfig\SystemConfigService;

class Config
{
    public function __construct(
        private SystemConfigService $systemConfigService,
    )

    public function setCredentials(string $clientId, string $clientSecret)
    {
        $this->systemConfigService->set('CompanyPluginName.config.clientId', $clientId);
        $this->systemConfigService->set('CompanyPluginName.config.clientSecret', $clientSecret);
    }
} 

Do you have any better ideas how I can authenticate to API - without creating and passing any credentials manually by plugin user? 😄️ I will be so grateful for ideas 🙏😊


Solution

  • EDIT: You need to run shopware-cli project config init first and supply the admin token which will then be stored in .shopware-project.yml


    I would not suggest to store a token clear text in the databae.

    You could use the shopware-cli tool

    https://sw-cli.fos.gg/commands/project/

    It has a ready made CLI command to call the API:

    shopware-cli project admin-api POST "/search/tax" -- \
    -d '{"limit": 1}' \
    -H 'Accept: application/json' \
    -H 'Content-Type: application/json'
    

    You can use the same command with --show-token to only obtain a token.