Notification texts go here Contact Us Buy Now!

How to hide Some API in django rest swagger?

In this blog post, we'll explore how to hide specific APIs from the Swagger documentation in Django REST. Swagger is a useful tool for generating documentation for REST APIs, but sometimes you may want to exclude certain APIs from the documentation for security or other reasons.

There are two main ways to hide APIs from Swagger in Django REST:

  1. Use swagger_schema = None

The swagger_schema attribute can be set to None to exclude a view from schema generation. This can be done in the view class itself or in a decorator. For example, in the following code, the get method of the MyView class is excluded from schema generation:

from drf_yasg.utils import swagger_auto_schema

class MyView(APIView):
    @swagger_auto_schema(auto_schema=None)
    def get(self, request):
        pass
  1. Use a decorator

Alternatively, you can use the @swagger_auto_schema decorator to exclude a view from schema generation. The following code is equivalent to the previous example:

from drf_yasg.utils import swagger_auto_schema

@swagger_auto_schema(auto_schema=None)
def get(self, request):
    pass

Both of these methods will effectively hide the specified API from the Swagger documentation.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.