Skip to content

Commit ba7426e

Browse files
committed
update client docs for baas use
1 parent b0c509f commit ba7426e

3 files changed

Lines changed: 81 additions & 11 deletions

File tree

README.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
# Base44 JavaScript SDK
22

3-
The Base44 SDK provides a JavaScript interface for building apps on the Base44 platform. When Base44 generates your app, the generated code uses the SDK to authenticate users, manage your app's data, interact with AI agents, and more. You can then use the same SDK to modify and extend your app.
3+
The Base44 SDK provides a JavaScript interface for building apps on the Base44 platform.
4+
5+
You can use it in two ways:
6+
7+
- **Inside Base44 apps**: When Base44 generates your app, the SDK is already set up and ready to use.
8+
- **External apps**: Use the SDK to build your own frontend or backend that uses Base44 as a backend service.
9+
10+
## Installation
11+
12+
Install the SDK via npm:
13+
14+
```bash
15+
npm install @base44/sdk
16+
```
17+
18+
> **Note**: In Base44-generated apps, the SDK is already installed for you.
419
520
## Modules
621

@@ -12,11 +27,15 @@ The SDK provides access to Base44's functionality through the following modules:
1227
- **[`connectors`](https://docs.base44.com/sdk-docs/interfaces/connectors)**: Manage OAuth connections and access tokens for third-party services.
1328
- **[`entities`](https://docs.base44.com/sdk-docs/interfaces/entities)**: Work with your app's data entities using CRUD operations.
1429
- **[`functions`](https://docs.base44.com/sdk-docs/interfaces/functions)**: Execute backend functions.
15-
- **[`integrations`](https://docs.base44.com/sdk-docs/type-aliases/integrations)**: Pre-built server-side functions for external services.
30+
- **[`integrations`](https://docs.base44.com/sdk-docs/type-aliases/integrations)**: Pre-built integrations for external services.
1631

17-
## Example
32+
## Quick starts
1833

19-
Here's a quick look at working with data in the SDK, using the `entities` module to create, update, and list records. In this example, we're working with a custom `Task` entity:
34+
How you get started depends on your context:
35+
36+
### Inside a Base44 app
37+
38+
In Base44-generated apps, the client is pre-configured. Just import and use it:
2039

2140
```typescript
2241
import { base44 } from "@/api/base44Client";
@@ -37,6 +56,45 @@ await base44.entities.Task.update(newTask.id, {
3756
const tasks = await base44.entities.Task.list();
3857
```
3958

59+
### External apps
60+
61+
When using Base44 as a backend for your own app, create and configure the client yourself:
62+
63+
```typescript
64+
import { createClient } from '@base44/sdk';
65+
66+
// Create a client for your Base44 app
67+
const base44 = createClient({
68+
appId: 'your-app-id' // Find this in the Base44 editor URL
69+
});
70+
71+
// Read public data (anonymous access)
72+
const products = await base44.entities.Products.list();
73+
74+
// Authenticate a user (token is automatically set)
75+
await base44.auth.loginViaEmailPassword('user@example.com', 'password');
76+
77+
// Now operations use the authenticated user's permissions
78+
const userOrders = await base44.entities.Orders.list();
79+
```
80+
81+
### Service role
82+
83+
For backend code that needs admin-level access, use the service role. Service role is only available in Base44-hosted backend functions:
84+
85+
```typescript
86+
import { createClientFromRequest } from 'npm:@base44/sdk';
87+
88+
Deno.serve(async (req) => {
89+
const base44 = createClientFromRequest(req);
90+
91+
// Access all data with admin-level permissions
92+
const allOrders = await base44.asServiceRole.entities.Orders.list();
93+
94+
return Response.json({ orders: allOrders });
95+
});
96+
```
97+
4098
## Learn more
4199

42100
For complete documentation, guides, and API reference, visit the **[Base44 SDK Documentation](https://docs.base44.com/sdk-getting-started/overview)**.
@@ -45,13 +103,17 @@ For complete documentation, guides, and API reference, visit the **[Base44 SDK D
45103

46104
### Build the SDK
47105

106+
Build the SDK from source:
107+
48108
```bash
49109
npm install
50110
npm run build
51111
```
52112

53113
### Run tests
54114

115+
Run the test suite:
116+
55117
```bash
56118
# Run all tests
57119
npm test
@@ -64,6 +126,7 @@ npm run test:coverage
64126
```
65127

66128
For E2E tests, create a `tests/.env` file with:
129+
67130
```
68131
BASE44_APP_ID=your_app_id
69132
BASE44_AUTH_TOKEN=your_auth_token

src/client.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ export type { Base44Client, CreateClientConfig, CreateClientOptions };
2525
*
2626
* This is the main entry point for the Base44 SDK. It creates a client that provides access to the SDK's modules, such as {@linkcode EntitiesModule | entities}, {@linkcode AuthModule | auth}, and {@linkcode FunctionsModule | functions}.
2727
*
28-
* Typically, you don't need to call this function because Base44 creates the client for you. You can then import and use the client to make API calls. The client takes care of managing authentication for you.
28+
* How you get a client depends on your context:
29+
* - **Inside a Base44 app:** The client is automatically created and configured for you. Import it from `@/api/base44Client`.
30+
* - **External app using Base44 as a backend:** Call `createClient()` directly in your code to create and configure the client.
2931
*
3032
* The client supports three authentication modes:
31-
* - **Anonymous**: Access modules anonymously without authentication using `base44.moduleName`. Operations are scoped to public data and permissions.
32-
* - **User authentication**: Access modules with user-level permissions using `base44.moduleName`. Operations are scoped to the authenticated user's data and permissions.
33-
* - **Service role authentication**: Access modules with elevated permissions using `base44.asServiceRole.moduleName`. Operations can access any data available to the app's admin. Can only be used in the backend. Typically, you create a client with service role authentication using the {@linkcode createClientFromRequest | createClientFromRequest()} function in your backend functions.
33+
* - **Anonymous**: Access modules without authentication using `base44.moduleName`. Operations are scoped to public data and permissions.
34+
* - **User authentication**: Access modules with user-level permissions using `base44.moduleName`. Operations are scoped to the authenticated user's data and permissions. Use `base44.auth.loginViaEmailPassword()` or other auth methods to get a token.
35+
* - **Service role authentication**: Access modules with elevated permissions using `base44.asServiceRole.moduleName`. Operations can access any data available to the app's admin. Only available in Base44-hosted backend functions. Create a client with service role authentication using {@linkcode createClientFromRequest | createClientFromRequest()}.
3436
*
3537
* For example, when using the {@linkcode EntitiesModule | entities} module:
3638
* - **Anonymous**: Can only read public data.
@@ -265,7 +267,7 @@ export function createClient(config: CreateClientConfig): Base44Client {
265267
/**
266268
* Provides access to service role modules.
267269
*
268-
* Service role authentication provides elevated permissions for server-side operations. Unlike user authentication, which is scoped to a specific user's permissions, service role authentication has access to the data and operations available to the app's admin.
270+
* Service role authentication provides elevated permissions for backend operations. Unlike user authentication, which is scoped to a specific user's permissions, service role authentication has access to the data and operations available to the app's admin.
269271
*
270272
* @throws {Error} When accessed without providing a serviceToken during client creation.
271273
*
@@ -296,7 +298,9 @@ export function createClient(config: CreateClientConfig): Base44Client {
296298
/**
297299
* Creates a Base44 client from an HTTP request.
298300
*
299-
* The client is created by automatically extracting authentication tokens from a request to a backend function. Base44 inserts the necessary headers when forwarding requests to backend functions.
301+
* This function is designed for use in Base44-hosted backend functions. For frontends and external backends, use {@linkcode createClient | createClient()} instead.
302+
*
303+
* When used in a Base44-hosted backend function, `createClientFromRequest()` automatically extracts authentication tokens from the request headers that Base44 injects when forwarding requests. The returned client includes service role access using `base44.asServiceRole`, which provides admin-level permissions.
300304
*
301305
* To learn more about the Base44 client, see {@linkcode createClient | createClient()}.
302306
*

src/client.types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ export interface CreateClientConfig {
4141
appId: string;
4242
/**
4343
* User authentication token. Used to authenticate as a specific user.
44+
*
45+
* Inside Base44 apps, the token is managed automatically. For external apps, use auth methods like {@linkcode AuthModule.loginViaEmailPassword | loginViaEmailPassword()} which set the token automatically.
4446
*/
4547
token?: string;
4648
/**
47-
* Service role authentication token. Use this in the backend when you need elevated permissions to access data available to the app's admin or perform admin operations. This token should be kept secret and never exposed in the app's frontend. Typically, you get this token from a request to a backend function using {@linkcode createClientFromRequest | createClientFromRequest()}.
49+
* Service role authentication token. Provides elevated permissions to access data available to the app's admin. Only available in Base44-hosted backend functions. Automatically added to client's created using {@linkcode createClientFromRequest | createClientFromRequest()}.
50+
* @internal
4851
*/
4952
serviceToken?: string;
5053
/**

0 commit comments

Comments
 (0)