Skip to main content

fragment-python

Fragment is the Ledger API for engineers that move money. Stop wrangling payment tables, debugging balance errors, and hacking together data pipelines. Start shipping the features that make a difference.

See CHANGELOG.md for release notes and upgrade guidance.

Installation

Using pip:

pip install fragment-python

Using poetry:

poetry add fragment-python

Usage

Get started by instantiating a Client from fragment.sdk.client. You can generate credentials using the Fragment dashboard

from fragment.sdk.client import Client

graphql_client = Client(
    client_id="<client id from the dashboard>",
    client_secret="<client secret from the dashboard>",
    api_url="<api url from the dashboard>",
    auth_url="<auth url from the dashboard>",
    auth_scope="<auth scope from the dashboard>",
  )

async def print_schema():
  get_schema_result = await graphql_client.get_schema("<Your schema key here>")
  print(get_schema_result.schema_.json())

import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(print_schema())

Read the Using custom queries section to learn how to use your own GraphQL queries with the SDK.

Using a synchronous client

If you prefer using a synchronous client instead of an async one, then:

from fragment.sync_sdk.client import Client

graphql_client = Client(
    client_id="<client id from the dashboard>",
    client_secret="<client secret from the dashboard>",
    api_url="<api url from the dashboard>",
    auth_url="<auth url from the dashboard>",
    auth_scope="<auth scope from the dashboard>",
  )

get_schema_result = graphql_client.get_schema("<Your schema key here>")
print(get_schema_result.schema_.json())

Examples

Post a Ledger Entry

To post a Ledger Entry defined in your Schema:

await graphql_client.add_ledger_entry(
  ik="some-ik",
  ledger_ik="your-ledger-ik",
  type="user_funds_account",
  posted="1968-01-01T16:45:00Z",
  parameters=dict(
    user_id="user-1",
    funding_amount="20000",
  )
)

Post a batch of Ledger Entries

To post a batch of Ledger Entries atomically:

from fragment.sdk.typed_entries import UserFundsAccountV1

await graphql_client.add_ledger_entries(
  entries=[
    UserFundsAccountV1(
      ik="some-ik-1",
      ledger_ik="your-ledger-ik",
      posted="1968-01-01T16:45:00Z",
      user_id="user-1",
      funding_amount="20000",
    ),
    UserFundsAccountV1(
      ik="some-ik-2",
      ledger_ik="your-ledger-ik",
      posted="1968-01-01T16:45:00Z",
      user_id="user-2",
      funding_amount="20000",
    ),
  ]
)

Construct the entries in the batch using the strongly-typed models for your Schema in the typed_entries module of your generated client.

Read a Ledger Account's Balance

To read a Ledger Account's balance:

from fragment.sdk.enums import CurrencyCode
from fragment.sdk.input_types import CurrencyMatchInput

await graphql_client.get_ledger_account_balance(
  ledger_ik="your-ledger-ik",
  path="liabilities/user:user-1/available",
  balance_currency=CurrencyMatchInput(code=CurrencyCode.USD),
)

Using custom queries

While the SDK comes with GraphQL queries out of the box, you may want to customize these queries for your product. In order to do that:

  1. Define your custom GraphQL queries in a GraphQL file. For example, in queries/custom-queries.graphql:
query getSchemaName($key: SafeString!) {
  schema(schema: { key: $key }) {
    key
    name
  }
}
  1. Run fragment-python-client-codegen to generate the GraphQL SDK client. GraphQL named queries are converted to snake_case to conform to Python's code conventions. Optionally, pass the --sync flag to generate a synchronous client instead of the default async GraphQL client.
fragment-python-client-codegen \
  --input-dir libs/fragment/queries/ \
  --target-package-name=custom_queries_package \
  --output-dir=libs/fragment
  1. Use the client from the generated package in your product! Apart from the custom query methods, this client is functionally identical to fragment.sdk.client.Client
from .libs.fragment.custom_queries_package.client import Client

graphql_client = Client(
    client_id="<client id from the dashboard>",
    client_secret="<client secret from the dashboard>",
    api_url="<api url from the dashboard>",
    auth_url="<auth url from the dashboard>",
    auth_scope="<auth scope from the dashboard>",
  )

async def print_schema_name():
  # Note that getSchemaName is converted to snake_case automatically
  response = await graphql_client.get_schema_name("<Your Schema Key>")
  print(response.schema_.key)

import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(print_schema())

Release files for fragment-python 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fragment-python 1.1.1
File Size Uploaded
fragment_python-1.1.1.tar.gz 84.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fragment-python 1.1.1
File Interpreter ABI Platform
fragment_python-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 207.6 kB

Release files / fragment_python-1.1.1.tar.gz

Download URL fragment_python-1.1.1.tar.gz
Size 84.3 kB
Tags Source
SHA-256 checksum
How to use checksums
552dabca9fd797bf7f4f13a221b7707900ad1a668c1c435591198a6a91b80afc
BLAKE2b-256 checksum
How to use checksums
edb9a24460e77a10499dc07d0ce3550d883a5b80de65a54e59b321ea3f8a330c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.10.14 Linux/6.17.0-1022-azure

Release files / fragment_python-1.1.1-py3-none-any.whl

Download URL fragment_python-1.1.1-py3-none-any.whl
Size 123.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b53f7d914c0a285a7f65e30b79b7cf00d28fce770ef59b18542d01681a2f27e3
BLAKE2b-256 checksum
How to use checksums
8e7117a8f36687f64c443e390092f0044a8134514bfa5a7c03f4210dd009b030
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.10.14 Linux/6.17.0-1022-azure

Release history Release notifications | RSS feed

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page