#api
30 snippets tagged with #api
HTTP Client with Axios Interceptors
Pre-configured Axios instance with request/response interceptors for auth headers, logging, and retry logic.
Best for: Consuming third-party APIs
Request Validation Schema Builder
Build a lightweight request validation layer with type inference for API endpoints.
Best for: API request body validation
Node.js Token Bucket Rate Limiter
Implement an in-memory token bucket rate limiter for controlling API request throughput.
Best for: Protecting APIs from abuse and DDoS
Native HTTP Server
Create a lightweight HTTP server using Node.js built-in http module with routing and JSON responses.
Best for: Lightweight API server without frameworks
Fastify Server Setup
Create a high-performance Fastify server with schema validation, plugins, and typed routes.
Best for: High-performance REST APIs
GraphQL Server with Type Definitions
Build a GraphQL API server with type definitions, resolvers, and query/mutation support.
Best for: GraphQL API development
Webhook Handler with Signature Verification
Process incoming webhooks with HMAC signature verification, replay protection, and idempotency.
Best for: GitHub/Stripe webhook processing
Cursor-Based Pagination API
Implement cursor-based pagination with forward/backward navigation, consistent ordering, and link headers.
Best for: REST API pagination
API Versioning Strategy
Implement API versioning with URL path, header-based, and content negotiation strategies.
Best for: API backward compatibility
Idempotency Key Middleware
Prevents duplicate request processing by caching responses keyed by the Idempotency-Key header.
Best for: Payment processing
OpenAPI Request Validator
Validates incoming request body against a JSON schema derived from an OpenAPI spec.
Best for: API input validation
Sliding Window Rate Limiter
Implements sliding window rate limiting that distributes limits more evenly than fixed windows.
Best for: API rate limiting
Optimistic Update Pattern
Apply UI changes immediately before server confirmation with automatic rollback on failure.
Best for: Like/upvote buttons
useMutation Hook for Side Effects
Handle mutations (POST/PUT/DELETE) with loading, error, reset, and optimistic update support.
Best for: Form submission handling
Type-Safe API Route Handler
Next.js App Router route handler with input validation, typed responses, and proper error handling.
Best for: CRUD API endpoints
Streaming API Response
Stream long-running API responses using ReadableStream and TransformStream for real-time data delivery.
Best for: AI response streaming
Next.js Route Handlers (API Routes)
Build RESTful API endpoints using Next.js App Router route handlers with typed responses.
Best for: Building REST APIs within a Next.js application
API Route Rate Limiting
Add rate limiting to Next.js API routes with sliding window, IP-based limits, and custom responses.
Best for: API abuse prevention
Route Handler Response Helpers
Common response patterns for Next.js route handlers with proper status codes and headers.
Best for: API route handlers
Route Handler with CORS Support
Add CORS headers to Next.js route handlers for cross-origin API access.
Best for: external API consumers
Webhook Handler with Signature Verification
Handle incoming webhooks with signature verification and idempotent processing.
Best for: payment webhooks
FastAPI Dependency Injection
Use FastAPI's dependency injection for database sessions, auth checks, and shared service logic.
Best for: Database session management
Mistral AI API Client in Python
Make chat and embedding requests to Mistral AI using the official Python SDK.
Best for: European AI compliance
BentoML Model Serving Service
Package and serve a scikit-learn model as a REST API with BentoML in Python.
Best for: model deployment
Vercel API — Trigger and List Deployments
Use the Vercel REST API to trigger deployments, list recent builds, and check deployment status.
Best for: Triggering deployments from CI or Slack bots
Rate Limiter — Token Bucket Algorithm
Implement a thread-safe rate limiter using the token bucket algorithm for API throttling.
Best for: API rate limiting per user or IP
Java Built-in HTTP Server
Create a lightweight HTTP server with com.sun.net.httpserver: routing, JSON responses, file serving.
Best for: Lightweight HTTP servers for testing and prototyping
Spring Boot — RestClient HTTP Calls
Make HTTP requests with Spring RestClient (6.1+): GET, POST, error handling, interceptors, and timeouts.
Best for: REST API consumption in Spring Boot services
HTTP Client with sttp
Make HTTP requests with sttp: GET, POST, headers, JSON bodies, and async backends.
Best for: REST API integration
Tapir Type-Safe API Endpoints
Define and serve HTTP API endpoints with Tapir: type-safe inputs, outputs, error handling, and docs.
Best for: Type-safe HTTP API definitions