UtilMeta - a meta backend framework for Python
Tags: python, django, restful, backend
Published: 2024-02-20 16:08:25

Hi~ I'm Xulin Zhou ([@voidZXL](https://twitter.com/voidZXL)), I recently open-sourced my 4-year project: [UtilMeta](https://utilmeta.com/py)

## Introduction
UtilMeta is a progressive meta-framework for backend applications, which efficiently builds declarative RESTful APIs based on the Python type annotation standard, and supports the integration of mainstream Python frameworks as runtime backend such as Django, Flask, FastAPI(Starlette) and Sanic

## Installation

```shell
pip install utilmeta
```

## Core features

### Declarative RESTful API

Using the declarative power from UtilMeta, you can easily write APIs with auto request validation, declarative ORM in UtilMeta can handle relational queries concisely without N+1 problem, both sync and async queries are supported

```python
from utilmeta.core import api, orm
from django.db import models

class User(models.Model):
    username = models.CharField(max_length=20, unique=True)

class Article(models.Model):
    author = models.ForeignKey(User, related_name="articles", on_delete=models.CASCADE)
    content = models.TextField()

class UserSchema(orm.Schema[User]):
    username: str
    articles_num: int = models.Count('articles')

class ArticleSchema(orm.Schema[Article]):
    id: int
    author: UserSchema
    content: str

class ArticleAPI(api.API):
    async def get(self, id: int) -> ArticleSchema:
        return await ArticleSchema.ainit(id)
```

if you request the ArticleAPI like `GET /article?id=1`, you will get the result like

```python
{
  "id": 1,
  "author": {
    "username": "alice",
    "articles_num": 3
  },
  "content": "hello world"
}
```
This conforms to what you declared, and the OpenAPI docs will be generated automatically like

![OpenAPI docs preview of ArticleAPI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ob0hnba4v6bofjru4p5d.png)



The concise declarative development can reduce a significant amount of code in building APIs, for example, UtilMeta implemented the [Realworld blog APIs](https://github.com/utilmeta/utilmeta-py-realworld-example-app) with only 600+ lines of code, while others:
![Realworld blog project: lines of code](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5t8o88d0585cplbi1it.jpg)



### Highly Flexible & Extensible
UtilMeta is highly flexible with a series of plugins including authentication (Session/JWT), CORS, rate limit, retry, and can be extended to support more features.

### Full-lifecycle DevOps Solution
The [UtilMeta Platform](https://utilmeta.com/) will provide the full-lifecycle DevOps solution for UtilMeta framework with API Docs, Debug, Logs, Monitoring and Alerts (currently in a Beta waitlist)

## Quick Start

You can start using UtilMeta by these case tutorials from easy to complex:

1. [BMI Calculation API](https://docs.utilmeta.com/py/en/tutorials/bmi-calc)
2. [User Login & RESTful API](https://docs.utilmeta.com/py/en/tutorials/user-auth)
3. [Realworld Blog Project](https://docs.utilmeta.com/py/en/tutorials/realworld-blog)

If you prefer to learn from a specific feature, you can refer to

* [Handle Request](https://docs.utilmeta.com/py/en/guide/handle-request): How to handle path, query parameters, request body, file upload, request headers and cookies.
* [API Class and Routing](https://docs.utilmeta.com/py/en/guide/api-route) How to use API class mounts to define tree-like API routing, and use hooks to easily reuse code between APIs, handle errors, and template responses.
* [Schema query and ORM](https://docs.utilmeta.com/py/en/guide/schema-query) How to use Schema to declaratively write the CRUD query, and ORM operations required by a RESTful interface.
* [API Authentication](https://docs.utilmeta.com/py/en/guide/auth): How to use Session, JWT, OAuth and other methods to authenticate the request of the interface, get the current request's user and simplify the login operation
* [Config, Run & Deploy](https://docs.utilmeta.com/py/en/guide/config-run): How to configure the run settings, startup, and deployment of a service using features such as declarative environment variables
* [Migrate from current project](https://docs.utilmeta.com/py/en/guide/migration) How to progressively integrate UtilMeta API to an existing backend project or migrate to UtilMeta

## Community

It's my 1st post in this community, if you find this project useful or interesting, please consider giving us a ⭐ in the [Github Repo](https://github.com/utilmeta/utilmeta-py) 🥰

and you can join our [Discord Server](https://discord.gg/JdmEkFS6dS) of UtilMeta to get the latest updates of the project and discuss with the community~