Skip to main content

JavaScript Client

APIs to interact with ComposeDB from JavaScript, TypeScript, or React.

Prerequisites


Installation


Install the ComposeDB client package:

pnpm add @composedb/client

If you’re using TypeScript, you may also need to install ComposeDB Types:

pnpm add -D @composedb/types

Configuration


Create a client instance by passing your server URL and your compiled composite:

// Import ComposeDB client

import { ComposeClient }from '@composedb/client'

// Import your compiled composite

import { definition }from './__generated__/definition.js'

// Create an instance of ComposeClient
// Pass the URL of your Ceramic server
// Pass reference to your compiled composite

const compose = new ComposeClient({ ceramic: 'http://localhost:7007', definition })

More details: ComposeClient

Queries


Executing Queries

Execute GraphQL Queries using the schema that is auto-generated from your compiled composite:

// Get account of authenticated user

await compose.executeQuery(`
query {
viewer {
id
}
}
`)

More details: executeQuery

Mutations


Enabling Mutations

tip

Before enabling mutations you must authenticate the user.

After you have an authenticated user, enable mutations by setting their authenticated account on the ComposeDB client:

// Assign the authorized did from your session to your client

compose.setDID(session.did)

Executing mutations

In your client, you can execute GraphQL mutations using the schema that is auto-generated from your compiled composite. Follow examples in the mutations guide.

Next Steps


Learn how to Authenticate Users →


ComposeDB’s JavaScript client optionally works with popular GraphQL clients: