If I understood your question you need to specify in your controller which bearer token you are using. In your case:
// swagger config
...
const config = new DocumentBuilder()
.setTitle('SWAGGER API')
.setVersion('1.0.0')
.addBearerAuth(
{
// I was also testing it without prefix 'Bearer ' before the JWT
description: `[just text field] Please enter token in following format: Bearer <JWT>`,
name: 'Authorization',
bearerFormat: 'Bearer', // I`ve tested not to use this field, but the result was the same
scheme: 'Bearer',
type: 'http', // I`ve attempted type: 'apiKey' too
in: 'Header'
},
'access-token', // This name here is important for matching up with @ApiBearerAuth() in your controller!
)
.build();
...
and in your controller:
@Get('/some-route')
@ApiBearerAuth('access-token') //edit here
@UseGuards(JwtAuthenticationGuard)
getData(
@ReqUser() user: User,
): void {
this.logger.warn({user});
}
Answer from Andrea Mugnai on Stack OverflowHello,
I am currently not able to get SwaggerUI to correctly handle a custom header:
// in route, the following Header is set
@ApiHeader({
name: 'X-Custom-Header',
description: 'Some custom header',
required: false,
})
...
@Post('route')
routeHandler(@Headers('X-Custom-Header') customHeader?: string) {..}The header is correctly showing up in SwaggerUI in the route, but any value passed in is ignored / not added as header.
The following request is sent:
curl -X 'POST' \ 'http://$host/route' \ -H 'accept: application/json' \ -d ''
What am I missing?
Appreciate the help, thanks in advance!
If I understood your question you need to specify in your controller which bearer token you are using. In your case:
// swagger config
...
const config = new DocumentBuilder()
.setTitle('SWAGGER API')
.setVersion('1.0.0')
.addBearerAuth(
{
// I was also testing it without prefix 'Bearer ' before the JWT
description: `[just text field] Please enter token in following format: Bearer <JWT>`,
name: 'Authorization',
bearerFormat: 'Bearer', // I`ve tested not to use this field, but the result was the same
scheme: 'Bearer',
type: 'http', // I`ve attempted type: 'apiKey' too
in: 'Header'
},
'access-token', // This name here is important for matching up with @ApiBearerAuth() in your controller!
)
.build();
...
and in your controller:
@Get('/some-route')
@ApiBearerAuth('access-token') //edit here
@UseGuards(JwtAuthenticationGuard)
getData(
@ReqUser() user: User,
): void {
this.logger.warn({user});
}
You can ignore the name and param of @ApiBearerAuth()
const config = new DocumentBuilder()
.setTitle('SWAGGER API')
.setVersion('1.0.0')
.addBearerAuth(
{
// I was also testing it without prefix 'Bearer ' before the JWT
description: `[just text field] Please enter token in following format: Bearer <JWT>`,
name: 'Authorization',
bearerFormat: 'Bearer', // I`ve tested not to use this field, but the result was the same
scheme: 'Bearer',
type: 'http', // I`ve attempted type: 'apiKey' too
in: 'Header'
}
)
.build();
And in your controller:
@Get('/some-route')
@ApiBearerAuth() //edit here
@UseGuards(JwtAuthenticationGuard)
getData(
@ReqUser() user: User,
): void {
this.logger.warn({user});
}
Global Headers for all Controllers (nestJs swagger) - Stack Overflow
how to custom request headers
Unable to add headers to a route in swagger documentation.
Authorization header not sent via the Swagger UI
Shortest way I have found is to do the following:
export function Headers() {
return applyDecorators(
ApiHeader({
name: 'header1',
description: "description"
}),
ApiHeader({
name: 'header2',
description: "description"
}),
ApiHeader({
name: 'header3',
description: "description"
})
);
}
@Headers()
@Controller('some-controller')
export class ContactsController {}
You can use DocumentBuilder.addGlobalParameters method.
Unfortunately, it's not described in the documentation, but here is my example:
SwaggerModule.setup(
'docs',
application, // your NestJS application created by NestFactory.create
SwaggerModule.createDocument(
application,
new DocumentBuilder()
.setTitle('My application')
.addGlobalParameters({
in: 'header',
required: false,
name: 'x-global-header',
schema: {
example: 'some value',
},
})
.build(),
),
);
» npm install @nestjs/swagger