--- url: "https://asyncapi.com/docs/tutorials/getting-started/security" titulek: "Adding security | AsyncAPI Initiative for event-driven APIs" h1: "Adding security" popis: "In production environments, your API may have to access a message broker that's protected by some auth mechanisms. Auth mechanism examples: User & password Certificates API keys OAuth 2 If you're " jazyk: "en" kanonicka: "" drobecky: [] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:10:04" znaku: 4558 slov: 573 kodovani: "utf-8" sha256: "705ebd1043409802c619ac8633e6adb7454fcf9febac8ec40cc6fd34245d483a" --- ##### On this page Conclusion # Adding security Found an error? Have a suggestion?[Edit this page on GitHub](https://github.com/asyncapi/website/blob/master/markdown/docs/tutorials/getting-started/security.md) In production environments, your API may have to access a message broker that's protected by some auth mechanisms. Auth mechanism examples: - User & password - Certificates - API keys - OAuth 2 If you're using AsyncAPI to define an API that connects to a message broker, you'll probably use user/password or certificates. Traditionally, message brokers are infrastructure pieces that serve an internal purpose, and they're not exposed to the public. That's why their security mechanisms are also simpler than what we're used to with REST APIs. However, AsyncAPI also helps you define your HTTP streaming APIs, and therefore, it supports more sophisticated mechanisms like OAuth2 or OpenID. Continuing with the `hello world` application example, let's learn how to define a simple security scheme (mechanism) for it. ``` asyncapi: 3.1.0 info: title: Hello world application version: '0.1.0' servers: production: host: broker.mycompany.com protocol: amqp description: This is "My Company" broker. security: - type: userPassword channels: hello: address: 'hello' messages: sayHelloMessage: $ref: '#/components/messages/hello-msg' goodbye: address: 'goodbye' messages: sayGoodbyeMessage: $ref: '#/components/messages/goodbye-msg' operations: receiveHello: action: 'receive' channel: $ref: '#/channels/hello' receiveGoodbye: action: 'receive' channel: $ref: '#/channels/goodbye' components: messages: hello-msg: payload: type: object properties: name: type: string sentAt: $ref: '#/components/schemas/sent-at' goodbye-msg: payload: type: object properties: sentAt: $ref: '#/components/schemas/sent-at' schemas: sent-at: type: string description: The date and time a message was sent. format: datetime ``` The example above shows how to specify that your server (a RabbitMQ broker) requires a user and a password to establish a connection. Let's break this down. There's a property in the server object called `security`. It's an array and can contain multiple security mechanisms. You chose to specify only one mechanism which is `userPassword`. A best practice is to put security details inside the `components.securitySchemes` section as it enables reusability across multiple servers. Below, you can see the same example, but this time, under server security, you see that `$ref` links to more security details located under the `user-password` object in `securitySchemes`. ``` asyncapi: 3.1.0 info: title: Hello world application version: '0.1.0' servers: production: host: broker.mycompany.com protocol: amqp description: This is "My Company" broker. security: - $ref: "#/components/securitySchemes/user-password" channels: hello: address: 'hello' messages: sayHelloMessage: $ref: '#/components/messages/hello-msg' goodbye: address: 'goodbye' messages: sayGoodbyeMessage: $ref: '#/components/messages/goodbye-msg' operations: receiveHello: action: 'receive' channel: $ref: '#/channels/hello' receiveGoodbye: action: 'receive' channel: $ref: '#/channels/goodbye' components: messages: hello-msg: payload: type: object properties: name: type: string sentAt: $ref: '#/components/schemas/sent-at' goodbye-msg: payload: type: object properties: sentAt: $ref: '#/components/schemas/sent-at' schemas: sent-at: type: string description: The date and time a message was sent. format: datetime securitySchemes: user-password: type: userPassword ``` ##### Hint Learn more about the several kinds of [security schemes](/docs/reference/specification/v2.5.0#securitySchemeObject). ## Conclusion You can now define what security mechanisms your application needs to connect to the server. You've also seen how to require a user and a password, which is the most common use case. At this point, you know AsyncAPI well enough to create a simple `Hello world application`. However, real use cases are more complicated than that. The following tutorials can teach you how to create real-world use cases from zero to production. [Go BackServers](/docs/tutorials/getting-started/servers) [Up NextCreate AsyncAPI document](/docs/tutorials/create-asyncapi-document) Was this helpful? Help us improve the docs by sharing your feedback. [Submit Feedback](https://github.com/asyncapi/website/issues/new?template=docs-feedback.yml&title=Docs%20Feedback%3A%20%2Fdocs%2Ftutorials%2Fgetting-started%2Fsecurity&labels=%F0%9F%93%91%20docs) OR [Create Issue on GitHub](https://github.com/asyncapi/website/issues/new/choose)