Search code examples
typescriptautorest

Sending bearer token to autorest typescript client


I am using autorest to generate TypeScript clients for accessing RESTful web services. All REST api looks for bearer token for authentication but I didn't find a way to pass token to autogenerated TypeScript clients.

I did try searching autorest documentation. It looks like I need to use ServiceClientCredentials but I didn't find any sample code.

Does anyone know how to use ServiceClientCredentials in TypeScript?

I use the following command to generate TypeScript clients

autorest --input-file=restapi.json --typescript --output-folder=./output --package-name="test-api" --package-version="0.1.0" --generate-metadata=true --add-credentials=true

Solution

  • I found the answer. I need to create an object of TokenCredentials and pass it in autogenerated client

    // Create token
      const tokenCredentials: TokenCredentials = new TokenCredentials(
        'some token'
      );
    
      // Add token and server url to service instance
      const service: AutoGeneratedSvc = new AutoGeneratedSvc(
        tokenCredentials,
        'server url'
      );