SDK
The following functions are provided to help you work with document and asset data inside your Satellite. They are part of the tools available when writing serverless functions in TypeScript and support common tasks such as interacting with the datastore, storage, and custom hook logic.
The SDK is provided by the @junobuild/functions library.
To add it to your project:
- npm
- yarn
- pnpm
npm i @junobuild/functions
yarn add @junobuild/functions
pnpm add @junobuild/functions
You have to follow the pace of the Juno release to ensure compatibility. Refer to the maintenance guide for instructions.
Datastoreβ
The following functions can be used to manage documents within the Datastore from your serverless functions.
setDocStoreβ
Sets a document in a collectionβs of the datastore. Use this to insert or update document data.
function setDocStore(params: SetDocStoreParams): void;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing the following fields:caller: The caller initiating the operation (RawUserIdorUserId).collection: The name of the collection where the document will be stored.key: The key identifying the document.doc: The document content including:data: A Uint8Array produced by encodeDocData.description(optional): A short description linked with the document.version(optional if new): An expected version number to prevent overwrite.
Returns:β
void
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation is rejected by the Satellite (e.g. due to a failed assertion or validation error).
getDocStoreβ
Retrieves a document from the datastore.
function getDocStore(params: GetDocStoreParams): OptionDoc;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The caller requesting the document (RawUserId or UserId).collection: The collection containing the document.key: The key identifying the document.
Returns:β
OptionDoc: The document if found, or undefined.
Throws:β
ZodErrorif the input schema is invalid.Errorif retrieval fails.
listDocsStoreβ
Lists documents in a collection using optional filters, pagination, and sorting.
function listDocsStore(params: ListDocsStoreParams): ListResults<Doc>;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The caller initiating the request (RawUserId or UserId).collection: The collection to list documents from.params: A ListParams object with pagination, filtering, or sorting options.
Returns:β
ListResults<Doc>: Matching documents and pagination metadata.
Throws:β
ZodErrorif the input schema is invalid.Errorif listing fails.
countDocsStoreβ
Counts documents matching filter criteria.
function countDocsStore(params: CountDocsStoreParams): bigint;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The caller initiating the operation.collection: The collection to count documents in.params: A ListParams object with filter and pagination options.
Returns:β
bigint: Number of documents matching the filter.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
countCollectionDocsStoreβ
Counts the total number of documents in a collection.
function countCollectionDocsStore(
params: CountCollectionDocsStoreParams
): bigint;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:collection: The collection to count documents in.
Returns:β
bigint: The total number of documents.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
deleteDocStoreβ
Deletes a document from the datastore.
function deleteDocStore(params: DeleteDocStoreParams): DocContext<OptionDoc>;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The caller initiating the deletion (RawUserId or UserId).collection: The collection where the document is stored.key: The key identifying the document.doc: The document deletion metadata including:version: The expected version of the document.
Returns:β
DocContext<OptionDoc>: Includes the key, collection, and optionally the previous document data.
Throws:β
ZodErrorif the input schema is invalid.Errorif validation fails or the document cannot be deleted.
deleteDocsStoreβ
Deletes all documents in a specific collection.
function deleteDocsStore(params: DeleteDocsStoreParams): void;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:collection: The collection to delete.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
deleteFilteredDocsStoreβ
Deletes documents matching filter criteria.
function deleteFilteredDocsStore(
params: DeleteFilteredDocsStoreParams
): DocContext<OptionDoc>[];
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The caller initiating the deletion.collection: The collection to target.params: A ListParams object with filter and pagination options.
Returns:β
DocContext<OptionDoc>[]: Context for each deleted document.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
Storageβ
The following functions can be used to manage assets within the Storage from your serverless functions.
setAssetHandlerβ
Sets or updates an asset using identity encoding (no compression).
function setAssetHandler(params: SetAssetHandlerParams): void;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing: -key: An object identifying the asset, including: -name: The asset filename (e.g., logo.png). -full_path: The asset path (e.g., /images/logo.png). -collection: The collection it belongs to. -owner: The caller principal. -token(optional): An access token if required. -description(optional): A short description. -content: A Uint8Array representing the raw content of the asset. -headers: An array of header fields to store with the asset.
Returns:β
void
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
getAssetStoreβ
Retrieves an asset from the Storage.
function getAssetStore(params: GetAssetStoreParams): OptionAsset;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The identity requesting the asset (RawUserIdorUserId).collection: The collection name.full_path: The asset path.
Returns:β
OptionAsset: The asset if found, orundefined.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
listAssetsStoreβ
Lists assets in a collection without their content.
function listAssetsStore(
params: ListAssetsStoreParams
): ListResults<AssetNoContent>;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The identity making the request.collection: The collection name.params: AListParamsobject to filter, sort, or paginate.
Returns:β
ListResults<AssetNoContent>: Matching assets without content chunks.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
countCollectionAssetsStoreβ
Counts all assets in a collection.
function countCollectionAssetsStore(
params: CountCollectionAssetsStoreParams
): bigint;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:collection: The collection name.
Returns:β
bigint: Number of assets in the collection.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
countAssetsStoreβ
Counts assets in a collection matching filter criteria.
function countAssetsStore(params: CountAssetsStoreParams): bigint;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The identity making the request.collection: The collection name.params: AListParamsobject to filter the count.
Returns:β
bigint: Number of matching assets.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
deleteAssetStoreβ
Deletes a single asset from the Storage.
function deleteAssetStore(params: DeleteAssetStoreParams): OptionAsset;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The identity initiating the deletion.collection: The collection name.full_path: The asset path to delete.
Returns:β
OptionAsset: The deleted asset, orundefined.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
deleteAssetsStoreβ
Deletes all assets in a collection.
function deleteAssetsStore(params: DeleteAssetsStoreParams): void;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:collection: The collection to delete.
Returns:β
void
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
deleteFilteredAssetsStoreβ
Deletes assets matching filters in a collection.
function deleteFilteredAssetsStore(
params: DeleteFilteredAssetsStoreParams
): OptionAsset[];
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The identity initiating the operation.collection: The collection name.params: AListParamsobject to define the filters.
Returns:β
OptionAsset[]: List of deleted assets.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
setAssetTokenStoreβ
Set or update an access token for an asset.
function setAssetTokenStore(params: SetAssetTokenStoreParams): void;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:caller: The identity initiating the deletion.collection: The collection name.full_path: The asset path to delete.token: The token to apply to the asset. Settingundefinedremoves the protection and makes the asset public.
Returns:β
void
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
getContentChunksStoreβ
Retrieves a specific chunk of an asset.
function getContentChunksStore(
params: GetContentChunksStoreParams
): Blob | undefined;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:encoding: TheAssetEncodingto retrieve from.chunk_index: The index of the chunk (starting from 0).memory: EitherHeaporStable.
Returns:β
Blob | undefined: The content chunk if found.
Throws:β
ZodErrorif the input schema is invalid.Errorif the operation fails.
Access Keysβ
The following functions allow you to inspect and assert the access keys of your Satellite.
getAccessKeysβ
Retrieves all access keys of the Satellite.
function getAccessKeys(): AccessKeys;
π¦ Import from @junobuild/functions/sdk
Returns:β
AccessKeys: The list of all access keys.
Throws:β
ZodErrorif the returned value does not match the expected schema.
getAdminAccessKeysβ
Retrieves only the admin access keys of the Satellite.
function getAdminAccessKeys(): AccessKeys;
π¦ Import from @junobuild/functions/sdk
Returns:β
AccessKeys: The list of admin access keys.
Throws:β
ZodErrorif the returned value does not match the expected schema.
isWriteAccessKeyβ
Checks if the given id is a non-expired access key with admin or write scope.
function isWriteAccessKey(params: AccessKeyCheckParams): boolean;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:id: The principal to check (RawUserIdorUserId).accessKeys: The list of access keys to verify against.
Returns:β
boolean: Whether the id is an access key with write permission.
Throws:β
ZodErrorif any input does not match the expected schema.
isValidAccessKeyβ
Checks if the given id is a non-expired access key regardless of scope (admin, write, or submit).
function isValidAccessKey(params: AccessKeyCheckParams): boolean;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:id: The principal to check (RawUserIdorUserId).accessKeys: The list of access keys to verify against.
Returns:β
boolean: Whether the id is a recognized access key.
Throws:β
ZodErrorif any input does not match the expected schema.
isAdminControllerβ
Checks if the given id is an admin access key and a controller known by the Internet Computer.
function isAdminController(params: AccessKeyCheckParams): boolean;
π¦ Import from @junobuild/functions/sdk
Parameters:β
params: An object containing:id: The principal to check (RawUserIdorUserId).accessKeys: The list of access keys to verify against.
Returns:β
boolean: Whether the id is an admin and a controller of the Satellite.
Throws:β
ZodErrorif any input does not match the expected schema.
Guardsβ
The following guard functions can be used to validate the caller in your custom serverless functions.
callerIsAdminβ
Guards that the caller is an admin access key of this satellite. Admin access keys have full management privileges and never expire.
function callerIsAdmin(): void;
π¦ Import from @junobuild/functions/sdk
Returns:β
void
Throws:β
ZodErrorif the caller is not an admin access key.
callerHasWritePermissionβ
Guards that the caller has write permission. Admin and editor access keys pass; submitter access keys do not.
function callerHasWritePermission(): void;
π¦ Import from @junobuild/functions/sdk
Returns:β
void
Throws:β
ZodErrorif the caller does not have write permission.
callerIsAccessKeyβ
Guards that the caller is any recognized access key of this satellite. Accepts admin, editor, and submitter access keys alike.
function callerIsAccessKey(): void;
π¦ Import from @junobuild/functions/sdk
Returns:β
void
Throws:β
ZodErrorif the caller is not a recognized access key.