How to test Shopware 6 APIs manually using PhpStorm REST API client

166 Views Asked by At

Shopware 6 provides a REST API, which uses a token that expires every 10 minutes.

When running manual API tests using PhpStorm this is annoying. How can this be done more efficiently?

1

There are 1 best solutions below

0
Alex On BEST ANSWER

PhpStorm can run multiple requests. We first define the client id and client secret in the Shopware 6 admin panel. Next we use this to obtain the short live token and then execute our request.

  1. Create a new test.http file in PhpStorm

  2. Insert the following:

    POST {{baseurl}}/api/oauth/token
    Content-Type: application/json
    
    {
        "grant_type": "client_credentials",
        "client_id": "{{client_id}}",
        "client_secret": "{{client_secret}}"
    }
    
    > {%
        client.global.set("token", response.body.access_token);
    %}
    
    ###
    POST {{baseurl}}/api/the-api-you-want-to-check
    Authorization: Bearer {{token}}
    Accept: application/json
    Content-Type: application/json
    
  3. Under the menu "run with" define a new environment

  4. Add you integration under System -> Integration in the Shopware Admin panel

    Adding secret

  5. Add the baseurl, the client_id and the client_secret to the environment:

    {
      "dev": {
        "baseurl": "https://example.com.local/",
        "client_id": "SWIADNHZQMVJMKFTRDLUCEOXTA",
        "client_secret": "MXRFbUxlMlcxRExvQmFFWXZnZjRXU1JqVVFRUWlES29yTldaQ1k",
      }
    }
    
  6. Execute the request

By adding the variables to the environment, you can also commit your test.http file to a dev folder or similar in your version control system to share it with other developers.