<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Omariba Collins on Medium]]></title>
        <description><![CDATA[Stories by Omariba Collins on Medium]]></description>
        <link>https://medium.com/@collinsomariba?source=rss-e138358d3db6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*MQQdHQhurSfUFFSp</url>
            <title>Stories by Omariba Collins on Medium</title>
            <link>https://medium.com/@collinsomariba?source=rss-e138358d3db6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:05:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@collinsomariba/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[API Throttling in Django Rest Framework]]></title>
            <link>https://medium.com/@collinsomariba/api-throttling-in-django-rest-framework-84b6b2026ca5?source=rss-e138358d3db6------2</link>
            <guid isPermaLink="false">https://medium.com/p/84b6b2026ca5</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[backend-development]]></category>
            <dc:creator><![CDATA[Omariba Collins]]></dc:creator>
            <pubDate>Sat, 28 Jan 2023 14:20:30 GMT</pubDate>
            <atom:updated>2023-01-28T18:49:29.007Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xnAuMajlXTnBrkijpLMf_w.png" /></figure><h3><strong>What is API throttling ?Also called Rate limiting.</strong></h3><p>It is controlling the rate of requests that clients can make to an API. This can be done for various reasons but mainly to prevent abuse of an API.</p><h3>How it works</h3><p>The IP address of an incoming request is used to generate a unique key to throttle against. For authenticated users, the user id is used to generate a unique key to throttle against.</p><h3>How to implement it</h3><p>In this implementation I assume you know how to setup a django project. I have setup a project with a blog app.</p><h4><strong>Set the throttling policy</strong></h4><p>Throttling classes are inbuilt into the Django Rest Framework hence no library needs to be installed.</p><p>In the <strong><em>settings.py</em> </strong>file add this.</p><pre><br>REST_FRAMEWORK = {<br>    &#39;DEFAULT_THROTTLE_CLASSES&#39;: [<br>        &#39;rest_framework.throttling.UserRateThrottle&#39;,<br>        &#39;rest_framework.throttling.AnonRateThrottle&#39;<br>    ],<br>    &#39;DEFAULT_THROTTLE_RATES&#39;: {<br>        &#39;user&#39;: &#39;40/minute&#39;,<br>        &#39;anon&#39;: &#39;20/minute&#39; <br>    }</pre><p>For this example Unauthenticated users will only be able to make 20 requests per minute while authenticated users can make 40 requests per minute.</p><h4>Add Throttling to a view</h4><p>You pass a throttle class to the <strong><em>throttle_clasess</em> </strong>attribute for class based views.</p><pre><br>from  rest_framework.generics import ListAPIView<br>from blog.serializers import BlogSerializer<br>from rest_framework.throttling import UserRateThrottle<br><br>class Blogview(ListAPIView):  <br>    serializer_class = BlogSerializer<br>    throttle_classes = [UserRateThrottle]</pre><p>In function based views use the <strong><em>throttle_classes </em></strong>decorator.</p><pre>@api_view([&#39;GET&#39;])<br>@throttle_classes([UserRateThrottle])<br>def blog_view(request, format=None):<br>    content = {<br>        &#39;status&#39;: &#39;request was permitted&#39;<br>    }<br>    return Response(content)</pre><p>Now for either of these views the authenticated users will be limited to only 40 requests per minute.</p><h4>Custom throttling classes</h4><p>You can use a <strong><em>throttles.py</em></strong> file in the app folder to write a custom throttling class for specific views as shown.</p><pre>from rest_framework.throttling import UserRateThrottle<br><br>class BlogRateThrottle(UserRateThrottle):<br>    scope = &#39;blog&#39;</pre><p>You also need to update the <strong><em>settings.py</em></strong> file to this.</p><pre><br>REST_FRAMEWORK = {<br>    &#39;DEFAULT_THROTTLE_CLASSES&#39;: [<br>        &#39;rest_framework.throttling.UserRateThrottle&#39;,<br>        &#39;rest_framework.throttling.AnonRateThrottle&#39;,<br>        &#39;blog.throttles.BlogRateThrottle&#39;, #new line<br>    ],<br>    &#39;DEFAULT_THROTTLE_RATES&#39;: {<br>        &#39;user&#39;: &#39;40/minute&#39;,<br>        &#39;anon&#39;: &#39;20/minute&#39; ,<br>        &#39;blog&#39;: &#39;10/minute&#39;, #new line<br>    }<br>}</pre><p>You can then add this custom throttle class to your view.</p><pre>from  rest_framework.generics import ListAPIView<br>from blog.serializers import BlogSerializer<br>from .throttles import BlogRateThrottle<br><br>class BlogView(ListAPIView):  <br>    serializer_class = BlogSerializer<br>    throttle_classes = [BlogRateThrottle]</pre><p>For this view only , authenticated users will now be limited to 10 requests per minute.</p><h4><strong>Throttling specific HTTP methods</strong></h4><p>In the example below only the <strong><em>post</em></strong> request in the <strong><em>viewset</em></strong> is throttled.</p><pre>from  rest_framework import viewsets<br>from blog.serializers import BlogSerializer<br>from blog.models import Blog<br>from rest_framework.throttling import UserRateThrottle<br><br><br>class BlogViewSet(viewsets. ModelViewSet):<br>    queryset = Blog.objects.all()<br>    serializer_class = BlogSerializer<br><br>    def get_throttles (self):<br>        if self.action == &#39;create&#39;:<br>            throttle_classes = [UserRateThrottle]<br>        else:<br>            throttle_classes = []<br>        return [throttle() for throttle in throttle_classes]</pre><h3>Conclusion</h3><p>API throttling is a cool and powerful feature , however you shouldn’t treat it as security feature against brute forcing or denial-of-service attacks due to reasons like spoofing of IP origins.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84b6b2026ca5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>