Django icon

Django Quick Start Guide

Get started with FacePing's face recognition API using Django

Prerequisites

Installation

First, create a new Django project and install the required packages:

mkdir faceping-django
cd faceping-django
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
pip install django python-dotenv requests

Step 1: Create Face Group

Create a new file views.py:

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
import requests
import os
from dotenv import load_dotenv

load_dotenv()
API_KEY = os.getenv('FACEPING_API_KEY')

@csrf_exempt
def create_group(request, group_name):
    try:
        response = requests.post(
            f'https://api.faceping.ai/groups/{group_name}',
            headers={'Authorization': f'Bearer {API_KEY}'}
        )
        response.raise_for_status()
        return JsonResponse({'message': 'Group created successfully'})
    except Exception as e:
        return JsonResponse({'error': str(e)}, status=500)

Step 2: Upload Face Image

Add to views.py:

@csrf_exempt
def upload_face(request, group_name):
    if not request.FILES.get('image'):
        return JsonResponse({'error': 'No image provided'}, status=400)

    try:
        image_file = request.FILES['image']
        files = {'image': (image_file.name, image_file.read(), image_file.content_type)}
        
        response = requests.post(
            f'https://api.faceping.ai/groups/{group_name}/faces',
            headers={'Authorization': f'Bearer {API_KEY}'},
            files=files
        )
        response.raise_for_status()
        return JsonResponse({'message': 'Face uploaded successfully'})
    except Exception as e:
        return JsonResponse({'error': str(e)}, status=500)

Step 3: Search for Faces

Add to views.py:

@csrf_exempt
def search_faces(request, group_name):
    if not request.FILES.get('image'):
        return JsonResponse({'error': 'No image provided'}, status=400)

    try:
        image_file = request.FILES['image']
        files = {'image': (image_file.name, image_file.read(), image_file.content_type)}
        
        response = requests.post(
            f'https://api.faceping.ai/groups/{group_name}/search',
            headers={'Authorization': f'Bearer {API_KEY}'},
            files=files
        )
        response.raise_for_status()
        return JsonResponse(response.json())
    except Exception as e:
        return JsonResponse({'error': str(e)}, status=500)

Add URL patterns in urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('groups/', views.create_group),
    path('groups//faces', views.upload_face),
    path('groups//search', views.search_faces),
]

Security Notes

  • Images are immediately discarded after vector conversion
  • Face vectors cannot be reverse-engineered into images
  • Always use HTTPS in production
  • Store your API keys in environment variables (.env file)

Ready to get started?

Take a look at the API documentation
API docs