Getting Started
This guide gets you from zero to a safe first API call.
1. Install or Load the SDK
Option A: npm (recommended)
npm install @jaynightmare/WeSDK
Option B: browser script
Load your hosted WeSDK.js bundle in the web-view page.
<script src="/path/to/WeSDK.js"></script>
2. Import the SDK
ESM
import sdk from "@jaynightmare/WeSDK";
CommonJS
const sdk = require("@jaynightmare/WeSDK");
Global browser usage
// window.wx / window.jWeixin / window.qq
const sdk = window.wx;
3. Configure at App Startup
sdk.config({
debug: false,
readyTimeout: 8000,
onDiagnostics(event) {
// Optional: pipe to telemetry
// event.event: invoke:start | invoke:response | bridge:not-ready | listener:*
},
});
4. Wait for Bridge Readiness
try {
await sdk.ready();
} catch (err) {
// No bridge or timeout path
console.error("Bridge not ready", err);
}
5. Check Capabilities Before Feature Use
const caps = await sdk.getCapabilities({
jsApiList: ["scanCode", "getLocation"],
});
if (!caps.checkResult.scanCode) {
// reason: bridge_unavailable | not_supported_by_client | check_js_api_unavailable
console.warn(caps.details.scanCode.reason);
}
6. Make Your First Safe API Call
if (await sdk.isApiSupported("scanCode")) {
const result = await sdk.scanCode({ onlyFromCamera: true });
console.log(result);
} else {
console.log("scanCode is unavailable in this runtime");
}
7. Optional Callback Interop (Legacy-Friendly)
v2 is Promise-first, but callback options still work for most APIs:
sdk.getNetworkType({
success(res) {
console.log(res);
},
fail(err) {
console.error(err);
},
complete() {
console.log("done");
},
});
Note: miniProgram routing methods use Promise semantics as the primary behavior.
Quick Integration Checklist
- Call
configonce at startup - Always
await ready()before bridge-dependent flows - Prefer capability checks for optional features
- Use
try/catcharound all async API calls - Use diagnostics hook in production for visibility
Next Steps
- Read Core Concepts
- Read Migration v1 to v2 if you are upgrading legacy code