Initialize
@cloudbase/js-sdk allows you to use JavaScript to access CloudBase services and resources on the web, such as PC Web pages and WeChat public platform H5.
The current @cloudbase/js-sdk version has launched v3 next. If you need to use v3 next, which supports browser, Node.js, React Native, and other general JavaScript runtime environments, please refer to the v3 next documentation.
Finish MCP setup first, then select a prompt to start your AI-native development journey
Prerequisites
Configure the Secure Domain Name
Before using @cloudbase/js-sdk, you need to configure the secure domain name, otherwise you will encounter a CORS error. For details, refer to: Secure Source
Configuration Steps:
- Go to CloudBase Platform / Environment Configuration / Security Configuration
- Add a website domain name (for example:
www.example.com) - Takes effect about 10 minutes after configuration
💡 Note:
- Only domain names in the secure domain name list can use the CloudBase JS SDK to protect your data security
- For local development, add
localhostor127.0.0.1and the port to the secure domain name list- If you encounter a CORS error, please check whether the secure domain name configuration is correct
Install and Initialize
Install the SDK
- Package Manager
- Full CDN Import
- On-demand CDN Import
# npm
npm install @cloudbase/js-sdk -S
# yarn
yarn add @cloudbase/js-sdk
Initialize SDK
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
region: "ap-shanghai", // Defaults to Shanghai region if not specified
});
latest indicates usage of the latest version, which can be set to a fixed version
<script src="https://static.cloudbase.net/cloudbase-js-sdk/latest/cloudbase.full.js"></script>
<script>
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
region: "ap-shanghai", // Defaults to Shanghai region if not specified
});
</script>
latest indicates usage of the latest version, which can be set to a fixed version
<!-- Core -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/latest/cloudbase.js"></script>
<!-- Auth module -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/latest/cloudbase.auth.js"></script>
<!-- Database module -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/latest/cloudbase.database.js"></script>
<!-- Data Model module -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/latest/cloudbase.model.js"></script>
<!-- Cloud Function module -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/latest/cloudbase.functions.js"></script>
<!-- Cloud Storage module -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/latest/cloudbase.storage.js"></script>
<!-- Real-time push module — must be imported after the database module -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/latest/cloudbase.realtime.js"></script>
<!-- AI module -->
<script src="https://static.cloudbase.net/cloudbase-js-sdk/latest/cloudbase.ai.js"></script>
<script>
// Register feature modules
registerAuth(cloudbase);
registerDatabase(cloudbase);
registerModel(cloudbase);
registerFunctions(cloudbase);
registerStorage(cloudbase);
registerRealtime(cloudbase);
registerAi(cloudbase);
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
region: "ap-shanghai", // Defaults to Shanghai region if not specified
});
</script>
💡 Note: Feature modules must be imported after the core, and the auth module must be included.
The latest version number can be viewed at NPM.
Initialization Parameters
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
env | string | Yes | - | CloudBase environment ID |
region | string | No | ap-shanghai | Region: ap-shanghai (default), ap-guangzhou, ap-singapore |
lang | string | No | zh-CN | Specified language: zh-CN (default), en-US |
accessKey | string | No | - | Anonymous user authentication parameter, can be exposed in the browser, used to access public resources |
⚠️ Note: The region of the currently used environment must match the specified region.
Login Authentication
js-sdk uses end-user permissions. You must log in before calling CloudBase capabilities.
- Publishable Key
- Anonymous Login
- Account Login
To use this feature, upgrade @cloudbase/js-sdk to version 2.21.0 or later.
Publishable Key can be exposed in the browser for requesting publicly accessible resources. Publishable Key is actually an anonymous user, which can effectively reduce MAU. For details, refer to the documentation.
Get Publishable Key
Go to CloudBase Platform / API Key Settings to generate a Publishable Key.
Use Publishable Key
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
accessKey: "Publishable Key", // Fill in the generated Publishable Key
});
⚠️ Note: Publishable Key can only be generated once, is permanently valid and cannot be deleted. Keep it safe.
For details, see: Anonymous Login
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
});
const { data, error } = await app.auth.signInAnonymously();
For details, see: Account Login
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
});
const { data, error } = await app.auth.signInWithPassword({
username: "your username",
password: "your password",
});
Initialization Examples
Singapore Region
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
region: "ap-singapore",
});
Use English Prompt
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // Replace with your environment ID
lang: "en-US",
});