# API restriction to only allow get methods for token users.

**URL:** <https://forum.djangoproject.com/t/api-restriction-to-only-allow-get-methods-for-token-users/6733>\
**Category:** Using Django\
**Created:** [February 23, 2021, 10:04pm UTC](https://forum.djangoproject.com/t/api-restriction-to-only-allow-get-methods-for-token-users/6733 "2021-02-23T22:04:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 23, 2021, 10:04pm UTC](https://forum.djangoproject.com/t/api-restriction-to-only-allow-get-methods-for-token-users/6733/1 "2021-02-23T22:04:55Z")

</div>

I am curious to know how I can restrict apis with token authentication so they can only access get methods of APIs.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 23, 2021, 11:02pm UTC](https://forum.djangoproject.com/t/api-restriction-to-only-allow-get-methods-for-token-users/6733/2 "2021-02-23T23:02:04Z")

</div>

I’d start with looking at the [Custom Permissions](https://www.django-rest-framework.org/api-guide/permissions/#custom-permissions) section of the Permissions page. One of the examples further down appears to be pretty close to what you’re looking to achieve.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 24, 2021, 1:41am UTC](https://forum.djangoproject.com/t/api-restriction-to-only-allow-get-methods-for-token-users/6733/3 "2021-02-24T01:41:10Z")

</div>

Thank you for the advice.  
Now I wrote that custom permission class and I tested it with a single ViewSet.  
I need to cover the all apis of whole app with this permission class so that every ViewSet allows token users only get method.  
At this point, my question is if I have to loop through all ViewSets or APIViews and add the custom permission class one by one or is there anyway that I can apply this custom permission in a single shot with something like middleware or whatever.  
I appreciate any advice.

---

<div class="post-metadata">

**Author:** ![CodenameTim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/codenametim/32/31507_2.png) [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Post date:** [February 24, 2021, 2:23am UTC](https://forum.djangoproject.com/t/api-restriction-to-only-allow-get-methods-for-token-users/6733/4 "2021-02-24T02:23:09Z")

</div>

The Permissions page has a section on [setting a default permission policy globally](https://www.django-rest-framework.org/api-guide/permissions/#setting-the-permission-policy). I suspect that’s what you want. If it doesn’t work for you, let me know where it’s falling short.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 24, 2021, 5:32am UTC](https://forum.djangoproject.com/t/api-restriction-to-only-allow-get-methods-for-token-users/6733/5 "2021-02-24T05:32:57Z")

</div>

Yes, I think that’s exactly what I want.  
But I noticed that global permissions are not applied to those API views or viewsets which have permission\_classes defined.  
Is it intended by Django?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 24, 2021, 12:57pm UTC](https://forum.djangoproject.com/t/api-restriction-to-only-allow-get-methods-for-token-users/6733/6 "2021-02-24T12:57:10Z")

</div>

Correct. A class-specified setting would override the global setting, not add to it.
