{"id":164,"date":"2026-03-03T09:26:56","date_gmt":"2026-03-03T04:26:56","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=164"},"modified":"2026-03-17T09:04:12","modified_gmt":"2026-03-17T04:04:12","slug":"authentication-in-apis","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/","title":{"rendered":"Authentication in APIs"},"content":{"rendered":"\n<p>Authentication in APIs ensures that only authorized users can access or modify data.<\/p>\n\n\n\n<p>In Django REST Framework (DRF), authentication verifies <strong>who the user is<\/strong>, and permissions decide <strong>what the user can do<\/strong>.<\/p>\n\n\n\n<p>APIs commonly use token-based authentication instead of session-based authentication.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why API Authentication is Important<\/h2>\n\n\n\n<p>Authentication helps:<\/p>\n\n\n\n<p>Protect sensitive data<br>Prevent unauthorized access<br>Secure user accounts<br>Control access to resources<br>Build professional and secure applications<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Types of Authentication in DRF<\/h2>\n\n\n\n<p>Django REST Framework supports multiple authentication methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Session Authentication<\/li>\n\n\n\n<li>Token Authentication<\/li>\n\n\n\n<li>JWT Authentication<\/li>\n\n\n\n<li>Basic Authentication<\/li>\n<\/ul>\n\n\n\n<p>Let\u2019s understand the most common ones.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Session Authentication<\/h2>\n\n\n\n<p>This is used mainly for web applications.<\/p>\n\n\n\n<p>It works with Django\u2019s login system.<\/p>\n\n\n\n<p>In <code>settings.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">REST_FRAMEWORK = {<br>    'DEFAULT_AUTHENTICATION_CLASSES': [<br>        'rest_framework.authentication.SessionAuthentication',<br>    ]<br>}<\/pre>\n\n\n\n<p>This works well for web browsers but is not ideal for mobile apps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Token Authentication (Most Common)<\/h2>\n\n\n\n<p>Token authentication is widely used for APIs and mobile applications.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Install Token Support<\/h3>\n\n\n\n<p>Add to <code>INSTALLED_APPS<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INSTALLED_APPS = [<br>    ...<br>    'rest_framework.authtoken',<br>]<\/pre>\n\n\n\n<p>Run migration:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python manage.py migrate<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Configure Authentication<\/h3>\n\n\n\n<p>In <code>settings.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">REST_FRAMEWORK = {<br>    'DEFAULT_AUTHENTICATION_CLASSES': [<br>        'rest_framework.authentication.TokenAuthentication',<br>    ],<br>}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Generate Token for User<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework.authtoken.models import Token<br>from django.contrib.auth.models import Useruser = User.objects.get(username=\"ali\")<br>token = Token.objects.create(user=user)<br>print(token.key)<\/pre>\n\n\n\n<p>You can also generate tokens automatically using signals.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Use Token in API Request<\/h3>\n\n\n\n<p>Client sends request with header:<\/p>\n\n\n\n<p>Authorization: Token your_token_here<\/p>\n\n\n\n<p>Now the API will authenticate the user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Protecting API Views<\/h2>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework.permissions import IsAuthenticated<br>from rest_framework.decorators import api_view, permission_classes<br>from rest_framework.response import Response@api_view(['GET'])<br>@permission_classes([IsAuthenticated])<br>def secure_api(request):<br>    return Response({\"message\": \"You are authenticated\"})<\/pre>\n\n\n\n<p>Only authenticated users can access this API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. JWT Authentication<\/h2>\n\n\n\n<p>JWT (JSON Web Token) is commonly used in modern applications.<\/p>\n\n\n\n<p>It generates:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Access Token<\/li>\n\n\n\n<li>Refresh Token<\/li>\n<\/ul>\n\n\n\n<p>JWT is:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Stateless<\/li>\n\n\n\n<li>Secure<\/li>\n\n\n\n<li>Scalable<\/li>\n<\/ul>\n\n\n\n<p>Popular package:<\/p>\n\n\n\n<p><code>djangorestframework-simplejwt<\/code><\/p>\n\n\n\n<p>Install:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install djangorestframework-simplejwt<\/pre>\n\n\n\n<p>JWT is commonly used in mobile apps and React\/Angular frontends.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Authentication vs Permission<\/h2>\n\n\n\n<p>Authentication \u2192 Who are you?<br>Permission \u2192 What are you allowed to do?<\/p>\n\n\n\n<p>Example permission classes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>IsAuthenticated<\/li>\n\n\n\n<li>IsAdminUser<\/li>\n\n\n\n<li>AllowAny<\/li>\n\n\n\n<li>IsAuthenticatedOrReadOnly<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example: Admin-Only API<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework.permissions import IsAdminUser@api_view(['GET'])<br>@permission_classes([IsAdminUser])<br>def admin_only(request):<br>    return Response({\"message\": \"Admin Access Only\"})<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for API Authentication<\/h2>\n\n\n\n<p>Use HTTPS<br>Do not expose tokens publicly<br>Use JWT for scalable apps<br>Set token expiration<br>Use proper permission classes<br>Never store passwords in plain text<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>Authentication in APIs ensures secure access to data.<\/p>\n\n\n\n<p>Django REST Framework provides powerful tools like Token Authentication and JWT to secure APIs efficiently.<\/p>\n\n\n\n<p>By combining authentication and permissions, you can build secure and professional backend systems.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FOR WEB DEVELOPMENT (PYWEB) > REST API Development > Authentication in APIs<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773720315812\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":91,"template":"","class_list":["post-164","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Authentication in APIs - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Secure your Django REST APIs with Token and JWT authentication. Manage permissions, protect data, and build safe backend systems.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Authentication in APIs - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Secure your Django REST APIs with Token and JWT authentication. Manage permissions, protect data, and build safe backend systems.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T04:04:12+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-in-apis\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-in-apis\\\/\",\"name\":\"Authentication in APIs - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-03T04:26:56+00:00\",\"dateModified\":\"2026-03-17T04:04:12+00:00\",\"description\":\"Secure your Django REST APIs with Token and JWT authentication. Manage permissions, protect data, and build safe backend systems.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-in-apis\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-in-apis\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/authentication-in-apis\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR WEB DEVELOPMENT (PYWEB) > REST API Development > Authentication in APIs\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Authentication in APIs - One Language. Endless Possibilities","description":"Secure your Django REST APIs with Token and JWT authentication. Manage permissions, protect data, and build safe backend systems.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/","og_locale":"en_US","og_type":"article","og_title":"Authentication in APIs - One Language. Endless Possibilities","og_description":"Secure your Django REST APIs with Token and JWT authentication. Manage permissions, protect data, and build safe backend systems.","og_url":"https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T04:04:12+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/","url":"https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/","name":"Authentication in APIs - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-03T04:26:56+00:00","dateModified":"2026-03-17T04:04:12+00:00","description":"Secure your Django REST APIs with Token and JWT authentication. Manage permissions, protect data, and build safe backend systems.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/authentication-in-apis\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR WEB DEVELOPMENT (PYWEB) > REST API Development > Authentication in APIs"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}