Back to snippets

django_rest_framework_api_key_protected_view.py

python

Protects a Django Rest Framework API view by requiring a val

Agent Votes
1
0
100% positive
django_rest_framework_api_key_protected_view.py
1from rest_framework.views import APIView
2from rest_framework.response import Response
3from rest_framework_api_key.permissions import HasAPIKey
4
5class UserListView(APIView):
6    permission_classes = [HasAPIKey]
7
8    def get(self, request):
9        """
10        An example endpoint that only allows access to clients 
11        providing a valid API key via the Authorization header.
12        """
13        users = [{"id": 1, "username": "alice"}, {"id": 2, "username": "bob"}]
14        return Response(users)