{"id":163,"date":"2026-03-03T09:22:50","date_gmt":"2026-03-03T04:22:50","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=163"},"modified":"2026-03-17T08:58:05","modified_gmt":"2026-03-17T03:58:05","slug":"creating-apis","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/creating-apis\/","title":{"rendered":"\u00a0Creating APIs"},"content":{"rendered":"\n<p>Creating APIs allows your application to communicate with mobile apps, frontend frameworks, and other systems using JSON data.<\/p>\n\n\n\n<p>In Django, APIs are commonly built using <strong>Django REST Framework (DRF)<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Install and Configure DRF<\/h2>\n\n\n\n<p>Install DRF:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install djangorestframework<\/pre>\n\n\n\n<p>Add it in <code>settings.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INSTALLED_APPS = [<br>    ...<br>    'rest_framework',<br>]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Create a Model<\/h2>\n\n\n\n<p>Example: Product Model<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.db import modelsclass Product(models.Model):<br>    name = models.CharField(max_length=100)<br>    price = models.IntegerField()<br>    description = models.TextField()    def __str__(self):<br>        return self.name<\/pre>\n\n\n\n<p>Run migrations:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">python manage.py makemigrations<br>python manage.py migrate<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Create a Serializer<\/h2>\n\n\n\n<p>Serializer converts model data into JSON format.<\/p>\n\n\n\n<p>Create <code>serializers.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework import serializers<br>from .models import Productclass ProductSerializer(serializers.ModelSerializer):<br>    class Meta:<br>        model = Product<br>        fields = '__all__'<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Create API Views<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Option 1: Function-Based API View<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework.decorators import api_view<br>from rest_framework.response import Response<br>from .models import Product<br>from .serializers import ProductSerializer@api_view(['GET'])<br>def product_list(request):<br>    products = Product.objects.all()<br>    serializer = ProductSerializer(products, many=True)<br>    return Response(serializer.data)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Add URL<\/h3>\n\n\n\n<p>In <code>urls.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.urls import path<br>from .views import product_listurlpatterns = [<br>    path('products\/', product_list),<br>]<\/pre>\n\n\n\n<p>Now visit:<\/p>\n\n\n\n<p class=\"has-black-color has-text-color has-link-color wp-elements-fa482ce61fe02167df8a16a284952367\"><a>http:\/\/127.0.0.1:8000\/products\/<\/a><\/p>\n\n\n\n<p>You will see JSON data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Create Full CRUD API<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Using ModelViewSet (Recommended)<\/h3>\n\n\n\n<p>In <code>views.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework import viewsets<br>from .models import Product<br>from .serializers import ProductSerializerclass ProductViewSet(viewsets.ModelViewSet):<br>    queryset = Product.objects.all()<br>    serializer_class = ProductSerializer<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Register Router<\/h3>\n\n\n\n<p>In <code>urls.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework.routers import DefaultRouter<br>from .views import ProductViewSetrouter = DefaultRouter()<br>router.register(r'products', ProductViewSet)urlpatterns = router.urls<\/pre>\n\n\n\n<p>Now you automatically get:<\/p>\n\n\n\n<p>GET \/products<br>POST \/products<br>GET \/products\/1<br>PUT \/products\/1<br>PATCH \/products\/1<br>DELETE \/products\/1<\/p>\n\n\n\n<p>This provides complete CRUD functionality.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example JSON Response<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">{<br>  \"id\": 1,<br>  \"name\": \"Laptop\",<br>  \"price\": 80000,<br>  \"description\": \"High performance laptop\"<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Adding Authentication (Optional)<\/h2>\n\n\n\n<p>In <code>settings.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">REST_FRAMEWORK = {<br>    'DEFAULT_PERMISSION_CLASSES': [<br>        'rest_framework.permissions.IsAuthenticated',<br>    ]<br>}<\/pre>\n\n\n\n<p>Now only logged-in users can access the API.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Testing APIs<\/h2>\n\n\n\n<p>You can test APIs using:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Browser (Browsable API)<\/li>\n\n\n\n<li>Postman<\/li>\n\n\n\n<li>Thunder Client (VS Code extension)<\/li>\n\n\n\n<li>Frontend applications<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Creating APIs is Important<\/h2>\n\n\n\n<p>APIs allow:<\/p>\n\n\n\n<p>Mobile app integration<br>Frontend-backend separation<br>Third-party integrations<br>Microservice architecture<br>Scalable system design<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>Creating APIs in Django REST Framework involves:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Creating a Model<\/li>\n\n\n\n<li>Creating a Serializer<\/li>\n\n\n\n<li>Creating a View or ViewSet<\/li>\n\n\n\n<li>Configuring URLs<\/li>\n<\/ol>\n\n\n\n<p>DRF simplifies the process and provides automatic CRUD functionality, authentication support, and JSON responses \u2014 making backend development fast and professional.<\/p>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773719903518\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\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 > Creating APIs<\/span><\/span><\/div>\n\n\n<p><\/p>\n","protected":false},"menu_order":90,"template":"","class_list":["post-163","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>\u00a0Creating APIs - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn to create RESTful APIs in Django REST Framework with Models, Serializers, ViewSets, CRUD, authentication, and JSON responses.\" \/>\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\/creating-apis\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Creating APIs - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn to create RESTful APIs in Django REST Framework with Models, Serializers, ViewSets, CRUD, authentication, and JSON responses.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/creating-apis\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T03:58:05+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-apis\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-apis\\\/\",\"name\":\"\u00a0Creating APIs - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-03T04:22:50+00:00\",\"dateModified\":\"2026-03-17T03:58:05+00:00\",\"description\":\"Learn to create RESTful APIs in Django REST Framework with Models, Serializers, ViewSets, CRUD, authentication, and JSON responses.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-apis\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-apis\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/creating-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 > Creating 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":"\u00a0Creating APIs - One Language. Endless Possibilities","description":"Learn to create RESTful APIs in Django REST Framework with Models, Serializers, ViewSets, CRUD, authentication, and JSON responses.","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\/creating-apis\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Creating APIs - One Language. Endless Possibilities","og_description":"Learn to create RESTful APIs in Django REST Framework with Models, Serializers, ViewSets, CRUD, authentication, and JSON responses.","og_url":"https:\/\/gigz.pk\/python\/lesson\/creating-apis\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T03:58:05+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/creating-apis\/","url":"https:\/\/gigz.pk\/python\/lesson\/creating-apis\/","name":"\u00a0Creating APIs - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-03T04:22:50+00:00","dateModified":"2026-03-17T03:58:05+00:00","description":"Learn to create RESTful APIs in Django REST Framework with Models, Serializers, ViewSets, CRUD, authentication, and JSON responses.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/creating-apis\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/creating-apis\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/creating-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 > Creating 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\/163","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=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}