import Tabs from '@theme/Tabs' import TabItem from '@theme/TabItem' import DocCardList from '@theme/DocCardList';
APIs to interact with ComposeDB from JavaScript, TypeScript, or React.
Install the ComposeDB client package:
<Tabs defaultValue="pnpm" groupId="npm-or-pnpm-or-yarn" values={[ {label: 'npm', value: 'npm'}, {label: 'pnpm', value: 'pnpm'}, {label: 'yarn', value: 'yarn'}, ]}>
npm install @composedb/clientpnpm add @composedb/clientyarn add @composedb/clientIf you’re using TypeScript, you may also need to install ComposeDB Types:
<Tabs defaultValue="pnpm" groupId="npm-or-pnpm-or-yarn" values={[ {label: 'npm', value: 'npm'}, {label: 'pnpm', value: 'pnpm'}, {label: 'yarn', value: 'yarn'}, ]}>
npm install -D @composedb/typespnpm add -D @composedb/typesyarn add -D @composedb/typesCreate 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
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
:::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:
<Tabs defaultValue="with-sessions" groupId="with-or-without-sessions" values={[ {label: 'With Sessions', value: 'with-sessions'}, {label: 'Without Sessions', value: 'without-sessions'}, ]}>
// Assign the authorized did from your session to your client
compose.setDID(session.did)// Call setDID method on ComposeClient instance
// Using authenticated did instance
compose.setDID(did)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.
Learn how to Authenticate Users →
ComposeDB’s JavaScript client optionally works with popular GraphQL clients: