Nexios Framework: OpenAPI Authentication Guide β
Nexios supports OpenAPI authentication, which allows you to secure your API endpoints with JSON Web Tokens (JWT) or other authentication mechanisms. Here's how you can set up OpenAPI authentication in your Nexios application
π¨ basic setup β
By default already provide bearerAuth
in OpenAPI documentation .
from nexios import NexiosApp
app = NexiosApp()
@app.get("/", security=[{"bearerAuth": []}])
async def get_root(req, res):
return {"message": "Hello, world!"}

π₯ Adding Multi Security Schemes β
from nexios import NexiosApp
app = NexiosApp()
@app.get("/", security=[{"bearerAuth": []}, {"apiKey": []}])
async def get_root(req, res):
return {"message": "Hello, world!"}
β οΈ Warning
You most register security scheme before using it.
π Registering Security Schemes β
π‘Tip
You can also access the openapi config from app.docs.cofig
object.
from nexios.openapi.models import APIKey
openapi_config.add_security_scheme(
"apiKeyAuth",
APIKey(
name="X-API-KEY",
**{
"in": "header",
"description": "My API key",
"type": "apiKey"
}
)
)
β οΈ Warning
Note : The dict used indead of passing the argugument directly. because in
is a reserved keyword in python.
π Authentication Types β
π‘οΈ JWT Bearer Authentication The most common method for modern APIs. Clients include a token in the Authorization header.
app.docs.config.add_security_scheme(
"jwtAuth", # Unique identifier
HTTPBearer(
type="http",
scheme="bearer",
bearerFormat="JWT",
description="π Requires valid JWT token in Authorization header"
)
)
π API Key Authentication β
For simpler authentication needs, using keys in headers, queries, or cookies.
app.docs.config.add_security_scheme(
"apiKeyAuth",
APIKey(
name="X-API-KEY", # Header/parameter name
**{
"in": "header", # Can be "header", "query", or "cookie"
"description": "π Access with your API key"
}
)
)
π OAuth2 Authentication β
app.docs.config.add_security_scheme(
"oauth2",
OAuth2(
flows=OAuthFlows(
password=OAuthFlowPassword(
tokenUrl="/auth/token",
scopes={
"read": "π Read access",
"write": "βοΈ Write access",
"admin": "π Admin privileges"
}
)
),
description="π OAuth2 password flow authentication"
)
)
π OAuth2 Scoped Routes β
Require specific permissions:
from nexios import NexiosApp
app = NexiosApp()
@app.get("/", security=[{"oauth2": ["read"]}])
async def get_root(req, res):
return {"message": "Hello, world!"}