Reference library

API design & gRPC

REST best practices, protobuf, API versioning, and backward-compatible service contracts.

2 matches
API design & gRPC medium

How to Build Cursor Pagination with Next and Prev Tokens in Python

A minimal cursor pagination implementation that returns next and previous cursor tokens for navigating a dataset.

pagination cursor api
Python
from pprint import pprint


def make_cursor(page):
    return f"page:{page:04d}"


def parse_cursor(cursor):
    _, page = cursor.split(":", 1)
    return int(page)


def paginate(all_items, page_size, cursor=None):
    start = parse_cursor(cursor) if cursor else 0
    end = start + page_size
    items = all_items[sta…
15 0 Open
API design & gRPC medium

How to Mock OAuth2 Bearer Token Auth Middleware in Python

Create a simple OAuth2 bearer token authentication middleware that verifies signed tokens and enforces scope-based access control.

oauth2 security middleware
Python
import hmac
import time
import base64
import json
from functools import wraps

VALID_TOKENS = {"test_token_123": {"user": "alice", "scope": "read:posts"}}


def generate_token(username: str) -> str:
    payload = {"user": username, "iat": int(time.time())}
    encoded = base64.urlsafe_b64encode(json.dumps(payload).enc…
13 0 Open

Browse by section

Each section groups closely related Python snippets.

API design & gRPC — Python code examples

What you will find here

This page collects api design & grpc snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.

Samples vs tutorials and challenges

Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.