initialization
Initialization
@cloudbase/js-sdk lets you use JavaScript to access cloudbase Service and resources on the Web (such as PC webpages, WeChat public platform H5) and Node.js server.
Currently, the @cloudbase/js-sdk version has launched the v3 next version. This version is fully compatible with v2 and aligns with the HTTP API.
If you need to use v2, refer to the v2 document.
If you need to use v1, refer to the v1 document
Installation and Initialization
Installing the SDK
- package manager
- CDN full import
- CDN on-demand import
# npm
npm install @cloudbase/js-sdk@next -S
# yarn
yarn add @cloudbase/js-sdk@next
Initialize SDK
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
region: "ap-shanghai", // Defaults to Shanghai region if not specified
});
3.x.x indicates usage of the next version, you can set it to a fixed version
<script src="https://static.cloudbase.net/cloudbase-js-sdk/3.0.1/cloudbase.full.js"></script>
<script>
const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
region: "ap-shanghai", // Defaults to the Shanghai region if not specified
});
</script>
3.x.x indicates usage of the next version, you can set it to a fixed version
Kernel
<script src="https://static.cloudbase.net/cloudbase-js-sdk/3.0.1/cloudbase.js"></script>
Login module
<script src="https://static.cloudbase.net/cloudbase-js-sdk/3.0.1/cloudbase.auth.js"></script>
Database module
<script src="https://static.cloudbase.net/cloudbase-js-sdk/3.0.1/cloudbase.database.js"></script>
Data model module
<script src="https://static.cloudbase.net/cloudbase-js-sdk/3.0.1/cloudbase.model.js"></script>
SCF module
<script src="https://static.cloudbase.net/cloudbase-js-sdk/3.0.1/cloudbase.functions.js"></script>
Cloud storage module
<script src="https://static.cloudbase.net/cloudbase-js-sdk/3.0.1/cloudbase.storage.js"></script>
Real-time push module, which must be imported after the database module is imported.
<script src="https://static.cloudbase.net/cloudbase-js-sdk/3.0.1/cloudbase.realtime.js"></script>
AI module
<script src="https://static.cloudbase.net/cloudbase-js-sdk/3.0.1/cloudbase.ai.js"></script>
<script>
// Registration feature module
registerAuth(cloudbase);
registerDatabase(cloudbase);
registerModel(cloudbase);
registerFunctions(cloudbase);
registerStorage(cloudbase);
registerRealtime(cloudbase);
registerAi(cloudbase);
const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
region: "ap-shanghai", // Defaults to the Shanghai region if not specified
});
</script>
💡 Note: The feature module must be introduced after the kernel, and the login module must be introduced.
The latest version number can go to NPM to view.
Initialize the parameter
| Field | Type | Required | Default Value | Description |
|---|---|---|---|---|
env | string | Yes | - | TCB 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 browser, used for requesting public accessed resources |
⚠️ Note: The region of the currently used environment must be consistent with the designated region information.
Login Authentication
js-sdk uses C-end user permission. You need to log in to call cloud development capability.
- 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 public accessed resources. Publishable Key is actually an anonymous user, which can effectively reduce MAU. For detailed introduction, refer to reference document.
Get Publishable Key
Go to Cloud Development Platform/API Key Configuration to generate Publishable Key.
Use Publishable Key
const app = cloudbase.init({
env: "your-env-id", // Replace this value 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 this value with your environment ID
});
const { data, error } = await app.auth.signInAnonymously();
For details, refer to Account login.
const app = cloudbase.init({
env: "your-env-id", // Replace this value with your environment ID
});
const { data, error } = await app.auth.signInWithPassword({
username: "your username",
password: "your password",
});
Initialization Example
Singapore region
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: "your-env-id", // Replace this value 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 this value with your environment ID
lang: "en-US",
});