| 1 | <a href="https://github.com/vitalik/django-ninja/issues/383"><img width="814" alt="SCR-20230123-m1t" src="https://user-images.githubusercontent.com/95222/214056666-585c0479-c122-4cb3-add4-b8844088ccdd.png"></a> |
| 2 | |
| 3 | |
| 4 | |
| 5 | <a href="https://github.com/vitalik/django-ninja/issues/383">^ Please read ^</a> |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | <p align="center"> |
| 11 | <a href="https://django-ninja.dev/"><img src="https://django-ninja.dev/img/logo-big.png"></a> |
| 12 | </p> |
| 13 | <p align="center"> |
| 14 | <em>Fast to learn, fast to code, fast to run</em> |
| 15 | </p> |
| 16 | |
| 17 | |
| 18 |  |
| 19 |  |
| 20 | [](https://badge.fury.io/py/django-ninja) |
| 21 | [](https://pepy.tech/project/django-ninja) |
| 22 | |
| 23 | # Django Ninja - Fast Django REST Framework |
| 24 | |
| 25 | **Django Ninja** is a web framework for building APIs with **Django** and Python 3.6+ **type hints**. |
| 26 | |
| 27 | |
| 28 | **Key features:** |
| 29 | |
| 30 | - **Easy**: Designed to be easy to use and intuitive. |
| 31 | - **FAST execution**: Very high performance thanks to **<a href="https://pydantic-docs.helpmanual.io" target="_blank">Pydantic</a>** and **<a href="/docs/docs/guides/async-support.md">async support</a>**. |
| 32 | - **Fast to code**: Type hints and automatic docs lets you focus only on business logic. |
| 33 | - **Standards-based**: Based on the open standards for APIs: **OpenAPI** (previously known as Swagger) and **JSON Schema**. |
| 34 | - **Django friendly**: (obviously) has good integration with the Django core and ORM. |
| 35 | - **Production ready**: Used by multiple companies on live projects (If you use django-ninja and would like to publish your feedback, please email [email protected]). |
| 36 | |
| 37 | |
| 38 | |
| 39 |  |
| 40 | |
| 41 | **Documentation**: https://django-ninja.dev |
| 42 | |
| 43 | --- |
| 44 | |
| 45 | ## Installation |
| 46 | |
| 47 | ``` |
| 48 | pip install django-ninja |
| 49 | ``` |
| 50 | |
| 51 | |
| 52 | |
| 53 | ## Usage |
| 54 | |
| 55 | |
| 56 | In your django project next to urls.py create new `api.py` file: |
| 57 | |
| 58 | ```Python |
| 59 | from ninja import NinjaAPI |
| 60 | |
| 61 | api = NinjaAPI() |
| 62 | |
| 63 | |
| 64 | @api.get("/add") |
| 65 | def add(request, a: int, b: int): |
| 66 | return {"result": a + b} |
| 67 | ``` |
| 68 | |
| 69 | |
| 70 | Now go to `urls.py` and add the following: |
| 71 | |
| 72 | |
| 73 | ```Python hl_lines="3 7" |
| 74 | ... |
| 75 | from .api import api |
| 76 | |
| 77 | urlpatterns = [ |
| 78 | path("admin/", admin.site.urls), |
| 79 | path("api/", api.urls), # <---------- ! |
| 80 | ] |
| 81 | ``` |
| 82 | |
| 83 | **That's it !** |
| 84 | |
| 85 | Now you've just created an API that: |
| 86 | |
| 87 | - receives an HTTP GET request at `/api/add` |
| 88 | - takes, validates and type-casts GET parameters `a` and `b` |
| 89 | - decodes the result to JSON |
| 90 | - generates an OpenAPI schema for defined operation |
| 91 | |
| 92 | ### Interactive API docs |
| 93 | |
| 94 | Now go to <a href="http://127.0.0.1:8000/api/docs" target="_blank">http://127.0.0.1:8000/api/docs</a> |
| 95 | |
| 96 | You will see the automatic interactive API documentation (provided by <a href="https://github.com/swagger-api/swagger-ui" target="_blank">Swagger UI</a> or <a href="https://github.com/Redocly/redoc" target="_blank">Redoc</a>): |
| 97 | |
| 98 |  |
| 99 | |
| 100 | |
| 101 | ## Sponsors |
| 102 | |
| 103 | <a href="https://www.sendcloud.com/"><img width="50%" alt="sendcloud-logo" src="https://github.com/user-attachments/assets/69a246d2-3dda-4473-a45c-9f0fc2f37c8c" /></a> |
| 104 | |
| 105 | <a href="mailto:[email protected]">Become a sponsor</a> |
| 106 | |
| 107 | |
| 108 | |
| 109 | ## What next? |
| 110 | |
| 111 | - Read the full documentation here - https://django-ninja.dev |
| 112 | - To support this project, please give star it on Github.  |
| 113 | - Share it [via Twitter](https://twitter.com/intent/tweet?text=Check%20out%20Django%20Ninja%20-%20Fast%20Django%20REST%20Framework%20-%20https%3A%2F%2Fdjango-ninja.dev) |
| 114 | - If you already using django-ninja, please share your feedback to [email protected] |