How I built API for iOS Feeds in 10 minutes

Just a short look what is possible with Django Rest Framework.

Recently I added an API to my iOS Feeds project and I would like to share what it took to create it. Because I am mostly involved in the iOS development community I wanted to share a story from "the other side".

If I had no experience with backends, I would think that these are always pretty complicated and involve a lot of moving parts.. So I want to show that that's not the case. If you select the right tools anyway.

iOS Feeds runs on Django and it wouldn't be that hard to build the API without any packages. However since Django Rest Framework (DRF) exists and it is wonderful package, I decided go to with it. While I have a lot of experience with Django itself, DRF is different story. Anyway, let's see how I added the API.

Django projects are usually separated into a smaller parts called "apps". So I created new API apps with this command:

python3 startapp api

And of course install the package:

pip install djangorestframework

This will do the basic scaffolding. Next I went by the DRF tutorial and created file.

To make this more digestible, I am going to show just the part that manages the videos endpoint. The articles are pretty much the same with a few extra bits.

from rest_framework import serializers
from feed.models import Video

class VideosSerializer(serializers.ModelSerializer):
    class Meta:
        model = Video
        fields = ['name', 'video_id', 'description', 'published', 'thumbnail_url']

The serializer is responsible for turning the database data into JSON. Here I specify the model and fields I want to serialize.

Next step is the file in the api app, which got automatically created.

from rest_framework import viewsets
from rest_framework import permissions
from api.serializers import VideosSerializer
from feed.models import Video

class VideoViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Video.objects.valid().order_by('-published')
    serializer_class = VideosSerializer
    permission_classes = [permissions.AllowAny]

Just a few imports and settings how the view set should behave. Once you have this defined, you can move to URL routes. I created file and wired up this VideoViewSet:

from django.urls import include, path
from rest_framework import routers
from api.views import VideoViewSet

router = routers.DefaultRouter()
router.register('videos', VideoViewSet)

urlpatterns = [
    path('', include(router.urls)),

Last step is to configure project level file:

urlpatterns = [
    # other routes omitted
    path('api/', include('api.urls'))


I still think it is pretty crazy that this is all that is needed. Django Rest Framework of course supports much more. Like pagination, throttling, authentification, and much more..

And you get this cool browsable API for free!

iOS Feeds browsable API

Filip Němeček profile photo


Filip Němeček @nemecek_f

iOS blogger and developer with interest in Python/Django. Telling other devs' stories with iOS Chat.