Panther API Guide#
This guide assumes you have successfully set up your project and created your first CRUD following the First CRUD guide.
In this guide, we discuss the features and structure of Panther's API system, including authentication, permissions, caching, throttling, middlewares, and more.
API Request Flow#
The typical flow of an API request in Panther is as follows:
Middlewares
├── Method
├── Authentication
├── Permissions
├── Throttling
├── Validate Input
├── Get Response From Cache
├── Call Endpoint
├── Set Response To Cache
Middlewares
Input Model#
You can validate incoming data using the input_model parameter. Pass a serializer to it, and Panther will send request.data to this serializer, placing the validated data in request.validated_data. As a result, request.validated_data will be an instance of your serializer.
Note:
request.datais validated only for 'POST', 'PUT', and 'PATCH' methods.
How do serializers work in Panther?
Refer to Serializer to learn more about serializers.
| app/serializers.py | |
|---|---|
1 2 3 4 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Output Model#
Use the output_model parameter to automatically serialize your API response data using a specified serializer. This ensures that the response structure is consistent and validated.
Example Serializer:
| app/serializers.py | |
|---|---|
1 2 3 4 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
Tip: Use
output_modelto ensure your API always returns data in the expected format. For OpenAPI documentation, see theoutput_schemasection.
Authentication#
To ensure that each request contains a valid authentication header, use the auth parameter.
- The
authparameter can be an any async function or a class with an async__call__method. - If you set
auth, Panther will use your specified authentication class or function. - If you do not set
auth, Panther will use the defaultAUTHENTICATIONfrom your config only if the request contains an authorization header. - If there is no authorization header in the request, authentication is bypassed,
request.userwill beNoneand you must rely on permissions to check the user and their authorization.
How do authentications work in Panther?
Refer to Authentications to learn more about authentications.
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 | |
Method#
You can specify which HTTP methods are allowed for an endpoint by setting methods in function-based APIs. Only the following methods are supported: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].
If a method is not allowed, a 405 status code will be returned.
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 8 | |
- Now this class only accepts
GETandPOSTrequests.
Permissions#
You can implement your authorization logic using permission classes or functions. Any async function or class with an async __call__ method can be used as a permission. Panther will call each permission (asynchronously).
Pass a list of permission callables to your API using the permissions parameter. If you pass a single permission, it will be automatically wrapped in a list.
Each permission must be async (either an async function or a class with an async
__call__).
Example Permission Function:
| app/permissions.py | |
|---|---|
1 2 3 4 | |
Example Permission Class:
| app/permissions.py | |
|---|---|
1 2 3 4 5 6 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
Cache#
Responses can be cached for a specific amount of time per request or IP. Caching is only applied to GET requests. The response's headers, data, and status code will be cached.
The cache is stored in Redis (if connected) or in memory. The cache key is based on the user ID or IP, request path, query parameters, and validated data:
'user_id or ip - request.path - hash of query param - request.validated_data'
The value of cache should be an instance of datetime.timedelta().
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
Throttling#
You can throttle requests using the Throttle class, either globally via the THROTTLING config or per API. The Throttle class has two fields: rate and duration.
If a user exceeds the allowed number of requests (rate) within the specified duration, they will receive a 429 Too Many Requests response and be banned for the duration.
When you set
throttlingon your API, it takes precedence over the defaultTHROTTLING, and the defaultTHROTTLINGwill not be executed.
Setting Default Throttling#
from datetime import timedelta
from panther.throttling import Throttle
# Users can only make 5 requests every minute
THROTTLING = Throttle(rate=5, duration=timedelta(minutes=1))
Throttling Per API#
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 | |
Customization#
Throttling works with request.user.id or request.client.ip. You can customize its behavior by overriding build_cache_key():
| app/throttlings.py | |
|---|---|
1 2 3 4 5 6 | |
Middlewares#
You can pass custom middlewares to specific APIs.
Example Middleware:
| app/middlewares.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 | |
How do middlewares work in Panther?
Refer to Middlewares to learn more about middlewares.
Output Schema#
The output_schema attribute is used when generating OpenAPI (Swagger) documentation.
It should be an instance of panther.openapi.OutputSchema, which specifies the desired response data structure and status code.
Example Serializer:
| app/serializers.py | |
|---|---|
1 2 3 4 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 8 | |
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 | |
How does OpenAPI work in Panther?
Refer to OpenAPI to learn more about OpenAPI.
File Handling#
Panther provides built-in support for file uploads through the File and Image classes.
Comprehensive File Handling Guide
For detailed information about file handling, including advanced features, best practices, and troubleshooting, see the dedicated File Handling documentation.
| app/apis.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |