Search code examples
reactjstypescriptswaggerbearer-tokenopenapi-generator

How to add Bearer token to headers of generated api endpoint from openapi-generator-cli


I am testing the openapi-generator-cli to generate services for a React client app from a swagger.json file.

Now some routes are protected and require a Bearer token in the headers. The token is returned after login.

Is there a possibility to add them as a config or header parameter when calling the generated methods of protected routes?


Solution

  • In my component I am implementing the Api with a service which takes the configuration as a parameter.

    myToken is a local variable where I stored the token previously.

    import * as Api from '../../api/src';
    const configParameters: Api.ConfigurationParameters = {
      headers: {
        'Authorization': 'Bearer ' + myToken,
      },
    };
    const configuration = new Api.Configuration(configParameters);
    const myService = new Api.MyApi(configuration);