Creating an endpoint for the root of our API

Right now we have endpoints for 'snippets' and 'users', but we don't have a single entry point to our API. To create one, we'll use a regular function-based view and the @api_view decorator we introduced earlier. In your snippets/views.py add:

  1. from rest_framework.decorators import api_view
  2. from rest_framework.response import Response
  3. from rest_framework.reverse import reverse
  4. @api_view(['GET'])
  5. def api_root(request, format=None):
  6. return Response({
  7. 'users': reverse('user-list', request=request, format=format),
  8. 'snippets': reverse('snippet-list', request=request, format=format)
  9. })

Two things should be noticed here. First, we're using REST framework's reverse function in order to return fully-qualified URLs; second, URL patterns are identified by convenience names that we will declare later on in our snippets/urls.py.