How Uni API Enables True Write‑Once‑Run‑Everywhere for Mini‑Programs
Uni API provides a framework‑agnostic, bilingual, TypeScript‑supported solution that unifies the fragmented APIs of major mini‑program containers and the web, allowing developers to write code once and run it across multiple platforms while reducing bundle size and simplifying maintenance.
With major mobile app vendors releasing their own mini‑program solutions, developers face the challenge of supporting multiple platforms with a single codebase. Existing cross‑platform frameworks (Rax, uniapp, Taro, Remax) address UI reuse, but the API layer lacks a standard, leading to duplicated and complex code.
After experiences with community group‑buying mini‑programs, Alibaba's digital agriculture team created Uni API to bridge API differences between mini‑program containers and the web, providing a solid foundation for "write once, run everywhere".
What is Uni API?
Supports Web, WeChat Mini‑Program, ByteDance Mini‑Program, Alipay Mini‑Program, Baidu Mini‑Program, Kuaishou Mini‑Program
No framework dependency
Bilingual documentation and rich examples
Implements 101 common APIs, with on‑demand loading and unused‑code removal
Comprehensive TypeScript declarations for better IDE support
Problems Uni API Solves
Inconsistent API prefixes (e.g., tt., wx.) across containers
Different input/output parameter specifications
Lack of TypeScript declarations in native APIs
Need to consult multiple platform docs and write compatibility code, inflating bundle size
Missing web implementations hinder debugging
Specification
Based on the WeChat Mini‑Program API spec, Uni API defines a unified set of 101 APIs and classifies them into four categories:
APIs identical across containers (same parameters and behavior)
APIs with differences that can be abstracted away
APIs with differences that cannot be unified
Container‑specific APIs not supported in the common set
For the first two categories, Uni API removes prefixes or normalizes parameters; for the third, it exposes the subset of capabilities while preserving differences; the fourth category is not included.
Input Normalization
Parameters are unified, and platform‑specific differences are handled via an _ext field. Example:
import showActionSheet from '@uni/action-sheet';
showActionSheet({
itemList: ['A', 'B', 'C'],
_ext: {
aliMiniApp: { title: 'Extra title for Alipay' },
wechatMiniProgram: { itemColor: '#ff1214' }
}
}).then(res => {
console.log(`Selected index: ${res.tapIndex}`);
});Only the relevant fields are applied in each container.
Output Normalization
Uni API standardizes return values, flattening differences while preserving any extra fields for developers to use.
Core Capabilities
True cross‑platform support that abstracts environment differences
Framework‑agnostic usage in native mini‑program projects or any cross‑platform framework
Promise‑based asynchronous APIs for easier maintenance
Full TypeScript declarations for IDE auto‑completion and error checking
Unified error handling
Extensive API coverage (100+ APIs, 70+ in the first release) with minimal bundle size
Supporting Features
Bilingual documentation covering every API and parameter
Real‑device demos for all supported platforms
CanIUse tool to detect API support at runtime
How to Use Uni API
Install either a small package for a single API or the full package for all APIs:
npm install --save @uni/storage
import { getStorageSync } from '@uni/storage';
getStorageSync({ key: 'key' }); npm install --save @uni/apis
import { confirm, storage } from '@uni/apis';
confirm({ content: 'Show modal' });
storage.getStorageSync({ key: 'key' });Use Babel or Rax plugins to tree‑shake unused code and achieve the smallest possible bundle.
Design Approach
Uni API is built atomically: each API is a module independent of container, resulting in 65 APIs across six containers (390 implementations) today, and a planned 101 APIs across six containers (606 modules) in the future.
Conclusion
Uni API addresses the misalignment of mini‑program and web APIs, offering a lightweight, cross‑platform solution that lets developers focus on business logic rather than compatibility, moving closer to a perfect write‑once‑run‑everywhere experience.
Related links: Uni API website , Rax website , GitHub repository , Issue tracker .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
