Initialization
@cloudbase/js-sdk enables you to access Cloudbase services and resources using JavaScript in Web environments, such as PC Web pages, WeChat Official Account H5, etc.
The current @cloudbase/js-sdk@latest version has been upgraded to v2. If you need to use v1, see v1 documentation.
Install and Initialize
Install the SDK
- Package Manager
- Full CDN Import
- CDN On-Demand Import
# npm
npm install @cloudbase/js-sdk -S
# yarn
yarn add @cloudbase/js-sdk
Initialize the SDK
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
region: region: "ap-shanghai", // Defaults to Shanghai region if not specified
});
${version} is set to latest to indicate using the latest version.
<script src="//static.cloudbase.net/cloudbase-js-sdk/${version}/cloudbase.full.js"></script>
<script>
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
region: region: "ap-shanghai", // Defaults to Shanghai region if not specified
});
</script>
${version} is set to latest to indicate using the latest version.
<!-- Kernel -->
<script src="//static.cloudbase.net/cloudbase-js-sdk/${version}/cloudbase.js"></script>
<!-- Login Module -->
<script src="//static.cloudbase.net/cloudbase-js-sdk/${version}/cloudbase.auth.js"></script>
<!-- Cloud Function Module -->
<script src="//static.cloudbase.net/cloudbase-js-sdk/${version}/cloudbase.functions.js"></script>
<!-- Cloud Storage Module -->
<script src="//static.cloudbase.net/cloudbase-js-sdk/${version}/cloudbase.storage.js"></script>
<!-- Database Module -->
<script src="//static.cloudbase.net/cloudbase-js-sdk/${version}/cloudbase.database.js"></script>
<!-- Real-time Push Module, must be imported after the Database Module -->
<script src="//static.cloudbase.net/cloudbase-js-sdk/${version}/cloudbase.realtime.js"></script>
<script>
// Register feature module
registerAuth(cloudbase);
registerFunctions(cloudbase);
registerStorage(cloudbase);
registerDatabase(cloudbase);
registerRealtime(cloudbase);
registerModel(cloudbase);
registerAi(cloudbase);
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
region: region: "ap-shanghai", // Defaults to Shanghai region if not specified
});
</script>
💡 Note: Feature modules must be imported after the kernel, and the login module must be imported.
The latest version number version can be found at NPM.
Initialization parameters
| 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 | Language: zh-CN (default), en-US |
accessKey | string | No | - | Authentication parameter for anonymous users, which can be exposed in browsers and is used to request publicly accessible resources |
⚠️ Note: The region of the environment currently in use must match the currently specified region information!
Login Authentication
The js-sdk uses client-side user permissions and requires login to invoke TCB capabilities.
- Anonymous Login
- Account Login
- Publishable Key
For details, see Anonymous Login
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
});
const auth = app.auth();
await auth.signInAnonymously();
For details, see Account Login
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
});
const auth = app.auth();
await auth.signIn({
username: "your username",
password: "your password",
});
To use this feature, please upgrade @cloudbase/js-sdk to version 2.21.0 or later.
Publishable Key can be exposed in browsers and is used to request publicly accessible resources. Publishable Key is essentially an anonymous user, which can effectively reduce MAU. For detailed information, see Documentation.
Obtain Publishable Key
You can go to TCB Platform/API Key Configuration to generate Publishable Key
Use Publishable Key
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
accessKey: accessKey: "Publishable Key", // fill in the generated Publishable Key
});
⚠️ Note: Publishable Key can only be generated once, and is long-term valid, cannot be deleted. Please keep it safe.
Initialization Sample
Singapore Region
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
region: "ap-singapore",
});
Use English Prompts
import cloudbase from "@cloudbase/js-sdk";
const app = cloudbase.init({
env: env: "your-env-id", // Replace with your environment id
lang: "en-US",
});