Skip to main content

Bachs SDK (Python)

Downloads Downloads Downloads

An unofficial python client SDK for BACHS

Features

  • Fully Typed.
  • Pydantic for payload and response data modeling.
  • Custom response data model injection.
  • Synchronous and Asynchronous clients

Installation

Requires Python version >=3.11

With uv

uv add bachs-sdk

With pip

pip install bachs-sdk

Usage

# You may provide your credentials through environment variables.
# The client automatically loads the following variables:
#
# BACHS_SECRET_KEY="<your bachs secret key>"
#
# Credentials passed to the client on instantiation take precedence over
# environment variables.

from bachs_sdk import BachsClient, AsyncBachsClient, Enviroment

# Instantiate the client
# Credentials may be passed directly if they are not available
# in the environment.
# May raise `MissingCredentialError` if no credentials was passed in 
# on instantiation or found in the environmental variables.
client = BachsClient()
# In a async context, you may use the async client
aclient = AsyncBachsClient()
# NOTE: The clients are configured to work in the sandboxed mode by default,
# you have to explicitly pass in `Enviroment.PRODUCTION` and use your production
# secret key i.e. `client = BachsClient(environment=Enviroment.PRODUCTION)`

# Now we create a payload for creating a checkout session.
body_data = {
            "customer": {"email": "adeyigbenga005@gmail.com", "name": "Gbenga Adeyi"},
            "product_cart": [
                {"product_id": "prod_199e781a66b04446a74d", "quantity": 1}
            ],
        }
body = CreateCheckoutSessionBody.model_validate(body_data)
# NOTE: `body` parameter of client methods are all pydantic models and you may 
# choose to instantiate it however you please. They follow a naming convention 
# of converting the method name (i.e. `create` in this case),and resource 
# (i.e. `CheckoutSession` in this ) they are for into PascalCase and 
# appending a `Body` to it.

# Now we call our client method designated for creating a checkout session
# with our payload. (Calling a client method that makes a request, may raise 
# `ClientError` You need to handle this. `client.checkout_sessions` has methods
# that maps to enpoints from the official API reference)
response = client.checkout_sessions.create(body)
# If you're using the async clients, the method names remain the same, they just
# need to be awaited (This only works in a async context. Note that we're using 
# `aclient `and not `client`).
response =await aclient.checkout_sessions.create(body)

# Use the response data in your application
print(response)
# NOTE: responses are pydantic models of type `APIResponse[T]` where `T` is the
# type of the `data` field. The response also includes fields like `http_status`
# `request_id` and `raw` when the request is successful else the response is of
# type `APIErrorResponse` which is also a pydantic model with the details of what
# went wrong with the request. The `raw` field of `APIResponse[T]`
# which is the exact response data gotten from Bachs without any serialization.
# It's also worth noting that you may pass a custom response model while calling
# the client method via the `response_model_class` parameter, for the most part,
# you'll only need to change the `T` of the `APIResponse` e.g if i have
# `MyCustomResponseData`, then `response_model_class=APIResponse[MyCustomResponseData]`.
# When the client is unable to serialize the data returned by Bachs, it sets the
# `data` field to unserialized data and logs a warning if `raise_serialization_error=False`,
# which is the default otherwise it raises a `ResponseSerializationError`, 
# the original data is still present in the `raw` field.

# Another example creating a test product
# Update your imports
from bachs_sdk import (
    BachsClient, 
    AsyncBachsClient, 
    Enviroment, 
    Currency, # newly added
    PriceType, # newly added
)

body_data = {
            "name": "Aura Farming Course edition 01",
            "description": "A demo product",
            "price": {
                "currency": Currency.NGN,
                "price_type": PriceType.FIXED,
                "amount": "1500.50",
                "currency_options": [],
            },
        }
body = CreateProductBody.model_validate(body_data)
response = client.products.create(body)
Client bindings Functionality
client.checkout_sessions provides methods that maps to checkout sessions endpoints
client.customers provides methods that maps to customers endpoints
client.media provides methods that maps to media upload endpoints
client.misc provides methods that maps to miscellaneous endpoints
client.payments provides methods that maps to payments endpoints
client.products provides methods that maps to products endpoints
client.refunds provides methods that maps to refunds endpoints
client.subscriptions provides methods that maps to subscriptions endpoints

Client bindings have the same name on both BachsClient and AsyncBachsClient

Client methods Functionality
client.checkout_sessions.create create a checkout session, see usage above
client.checkout_sessions.get retrieve a checkout session
client.customers.create create a customer
client.customers.get retrieve a customer
client.customers.update update a customer's information
client.customers.list retrieve a list of customers with pagination
client.media.upload upload a media file
client.media.get retrieve an uploaded media file
client.media.delete delete an uploaded media file
client.misc.get_balances retrieve account balances
client.misc.list_payment_methods retrieve a list of supported payment methods
client.misc.list_payment_rails retrieve a list of payment rails
client.misc.list_payout_supported_currencies retrieve a list of payout supported currencies
client.misc.list_supported_currencies retrieve a list of supported currencies
client.payments.list retrieve a list of payments with pagination support
client.payments.get retrieve a payments (transaction)
client.products.create create a product
client.products.get retrieve a product
client.products.update update the details of a product
client.products.list retrieve a list of products with pagination support
client.products.archive archive a product
client.products.unarchive unarchive a product
client.refunds.create create a refund
client.refunds.list retrieve a list of refunds with pagination support
client.refunds.get retrieve a refund
client.subscriptions.list retrieve a list of subscriptions with pagination support
client.subscriptions.get retrieve a subscription
client.subscriptions.update update a subscription
client.subscriptions.delete delete a subscription

NOTE

This SDK is still under active development and the APIs may change. All you'll ever need to import from the SDK, can be imported from the top level (i.e.from bachs_sdk import x,y,z) for ease of you but might change if the need arises. Doc strings documentation coming soon.


Issues and Contributions

If you discover bugs, missing endpoints, incorrect typings, or any other issues, please open an issue or submit a pull request. Contributions are welcome.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bachs_sdk-0.0.0.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bachs_sdk-0.0.0-py3-none-any.whl (37.3 kB view details)

Uploaded Python 3

File details

Details for the file bachs_sdk-0.0.0.tar.gz.

File metadata

  • Download URL: bachs_sdk-0.0.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.30 {"installer":{"name":"uv","version":"0.11.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for bachs_sdk-0.0.0.tar.gz
Algorithm Hash digest
SHA256 622a97092efa60f3ec3db6e927d207233c3fc04fd06829511c0eafa8e84fda1c
MD5 87c6131fd7feada0428eeb813ee0bb0d
BLAKE2b-256 423a6292773a690f83ab225a13e71ee72a80cb917302eeba33a267ad6f6f2e20

See more details on using hashes here.

File details

Details for the file bachs_sdk-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: bachs_sdk-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 37.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.30 {"installer":{"name":"uv","version":"0.11.30","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for bachs_sdk-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0a5e116e0aba4327b1e557a7cfa99762ce71b8fb35ff18aabb1d2d4aaf2a4e4
MD5 d75242b9c4d359e0f57c35b1e0306121
BLAKE2b-256 aedba21cc19842ef48565dd36b19442d0f06c91eebaece3d405386aafb57106f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page