# Using the @daml/ledger library without the @daml/react library

**URL:** <https://forum.canton.network/t/using-the-daml-ledger-library-without-the-daml-react-library/6381>\
**Category:** App Development\
**Created:** [April 5, 2023, 8:08am UTC](https://forum.canton.network/t/using-the-daml-ledger-library-without-the-daml-react-library/6381 "2023-04-05T08:08:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gyorgybalazsi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gyorgybalazsi/32/684_2.png) [@gyorgybalazsi](https://forum.canton.network/u/gyorgybalazsi)\
**Post date:** [April 5, 2023, 8:08am UTC](https://forum.canton.network/t/using-the-daml-ledger-library-without-the-daml-react-library/6381/1 "2023-04-05T08:08:34Z")

</div>

Does anybody use the @daml/ledger library without the @daml/react library?

I would appreciate a small example to help me get started.

The docs don’t elaborate on this, advising users to check out how the @daml/react library uses the @daml/ledger library, but it’s not straightforward to separate out the non React specific parts of the code.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [April 5, 2023, 8:33am UTC](https://forum.canton.network/t/using-the-daml-ledger-library-without-the-daml-react-library/6381/2 "2023-04-05T08:33:53Z")

</div>

Is there something in particular that you are struggling with?

---

<div class="post-metadata">

**Author:** ![gyorgybalazsi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gyorgybalazsi/32/684_2.png) [@gyorgybalazsi](https://forum.canton.network/u/gyorgybalazsi)\
**Post date:** [April 5, 2023, 8:40am UTC](https://forum.canton.network/t/using-the-daml-ledger-library-without-the-daml-react-library/6381/3 "2023-04-05T08:40:45Z")

</div>

I started with looking at this piece of code and I can see that already the type of the DamlLedger object uses React: `DamlLedger: React.FC<LedgerProps>;`

```haskell
import React from "react";
import { ContractId, Party, Template, TemplateOrInterface } from "@daml/types";
import Ledger, { CreateEvent, Query, StreamCloseEvent, QueryResult, User } from "@daml/ledger";
export { QueryResult } from "@daml/ledger";
/**
 * React props to initiate a connect to a Daml ledger.
 */
export declare type LedgerProps = {
    token: string;
    httpBaseUrl?: string;
    wsBaseUrl?: string;
    user?: User;
    party: Party;
    reconnectThreshold?: number;
};
/**
 * The result of a ``fetch`` against the ledger.
 *
 * @typeparam T The contract template type of the query.
 * @typeparam K The contract key type of the query.
 * @typeparam I The template id type.
 */
export declare type FetchResult<T extends object, K, I extends string> = {
    /** Contracts of the given contract template and key. */
    contract: CreateEvent<T, K, I> | null;
    /** Indicator for whether the fetch is executing. */
    loading: boolean;
};
/**
 * The result of a streaming ``fetchByKeys`` against the ledger.
 *
 * @typeparam T The contract template type of the query.
 * @typeparam K The contract key type of the query.
 * @typeparam I The template id type.
 */
export declare type FetchByKeysResult<T extends object, K, I extends string> = {
    /** Contracts of the given contract template and key. */
    contracts: (CreateEvent<T, K, I> | null)[];
    /** Indicator for whether the fetch is executing. */
    loading: boolean;
};
/**
 * A LedgerContext is a React context that stores information about a Daml Ledger
 * and hooks necessary to use it.
 */
export declare type LedgerContext = {
    DamlLedger: React.FC<LedgerProps>;
    useParty: () => Party;
    useUser: () => User;
    useLedger: () => Ledger;
    useQuery: <T extends object, K, I extends string>(template: TemplateOrInterface<T, K, I>, queryFactory?: () => Query<T>, queryDeps?: readonly unknown[]) => QueryResult<T, K, I>;
    useFetch: <T extends object, K, I extends string>(template: TemplateOrInterface<T, K, I>, contractId: ContractId<T>) => FetchResult<T, K, I>;
    useFetchByKey: <T extends object, K, I extends string>(template: Template<T, K, I>, keyFactory: () => K, keyDeps: readonly unknown[]) => FetchResult<T, K, I>;
    useStreamQuery: <T extends object, K, I extends string>(template: TemplateOrInterface<T, K, I>, queryFactory?: () => Query<T>, queryDeps?: readonly unknown[], closeHandler?: (e: StreamCloseEvent) => void) => QueryResult<T, K, I>;
    useStreamQueries: <T extends object, K, I extends string>(template: TemplateOrInterface<T, K, I>, queryFactory?: () => Query<T>[], queryDeps?: readonly unknown[], closeHandler?: (e: StreamCloseEvent) => void) => QueryResult<T, K, I>;
    useStreamFetchByKey: <T extends object, K, I extends string>(template: Template<T, K, I>, keyFactory: () => K, keyDeps: readonly unknown[], closeHandler?: (e: StreamCloseEvent) => void) => FetchResult<T, K, I>;
    useStreamFetchByKeys: <T extends object, K, I extends string>(template: Template<T, K, I>, keyFactory: () => K[], keyDeps: readonly unknown[], closeHandler?: (e: StreamCloseEvent) => void) => FetchByKeysResult<T, K, I>;
    useReload: () => () => void;
};
/**
 * Create a [[LedgerContext]]. One should use this function, instead of the default [[DamlLedger]],
 * where one needs to be able to nest ledger interactions, by different parties or connections, within
 * one React application.
 *
 * @param contextName Used to refer to a context in case of errors.
 */
export declare function createLedgerContext(contextName?: string): LedgerContext;

```

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [April 5, 2023, 8:47am UTC](https://forum.canton.network/t/using-the-daml-ledger-library-without-the-daml-react-library/6381/4 "2023-04-05T08:47:14Z")

</div>

Right, you need to instead use the methods from [default |](https://docs.daml.com/app-dev/bindings-ts/daml-ledger/classes/default.html).

Roughly something like this:

```haskell
import Ledger, { CreateEvent, LedgerOptions } from '@daml/ledger';
const ledger = new Ledger(yourLedgerOptions)
await ledger.create(…)

```

---

<div class="post-metadata">

**Author:** ![gyorgybalazsi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gyorgybalazsi/32/684_2.png) [@gyorgybalazsi](https://forum.canton.network/u/gyorgybalazsi)\
**Post date:** [April 5, 2023, 8:49am UTC](https://forum.canton.network/t/using-the-daml-ledger-library-without-the-daml-react-library/6381/5 "2023-04-05T08:49:48Z")

</div>

This looks good, thank you!

---

<div class="post-metadata">

**Author:** ![gyorgybalazsi](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gyorgybalazsi/32/684_2.png) [@gyorgybalazsi](https://forum.canton.network/u/gyorgybalazsi)\
**Post date:** [April 5, 2023, 10:29am UTC](https://forum.canton.network/t/using-the-daml-ledger-library-without-the-daml-react-library/6381/6 "2023-04-05T10:29:25Z")

</div>

Recording for posterity, this is my first working nodejs program using the the @daml/ledger library, thanks again:

```haskell
import Ledger, { CreateEvent, LedgerOptions } from '@daml/ledger';

var jwt = require('jwt-simple');

var loginName = "Alice";
var ledgerId = "sandbox";

var payload = {
    "https://daml.com/ledger-api": {
      "ledgerId": ledgerId,
      "applicationId": 'create-daml-app',
      "actAs": [loginName]
    }
}
var secret = 'xxx';
var token = jwt.encode(payload, secret);

let ledgerOptions: LedgerOptions = {
    httpBaseUrl: "http://127.0.0.1:7575/",
    token:token
}
const ledger = new Ledger(ledgerOptions);
// await ledger.create(…)

async function printAlice() {
    var alice = await ledger.getUser("alice");
    console.log(alice);
}

printAlice()

```

The result:

```haskell
{
  userId: 'alice',
  primaryParty: 'Alice::122020032b4df0224c7c3003ace3ba4e3bcf3c49083638fbdbbb6663c95ea02192d3'
}

```
