frappex-client v0.1.1

Start here

Getting started#

This guide takes you from nothing to reading and writing documents on a Frappe or ERPNext site. It takes about five minutes.

Requirements#

  • Python 3.10 or newer.
  • A Frappe or ERPNext site, version 14, 15 or 16, reachable over HTTPS.
  • A user on that site with permission for the DocTypes you want to use.

1. Install#

pip install frappex-client
uv add frappex-client
poetry add frappex-client

frappex-client installs one dependency, httpx.

2. Create an API key#

An API key and secret let a program act as a user without a password. Create them on the site:

  1. Open User and pick the user the program should act as.
  2. Open the Settings tab and find the API Access section.
  3. Click Generate Keys. Copy the API Secret now: it is shown only once.
  4. The API Key stays visible on the user record.

Give programs their own user

Create a dedicated user, such as integration@example.com, with only the roles the program needs. You can then revoke its keys or narrow its permissions without touching a real person's account.

3. Store the credentials safely#

Never put the secret in source code. Read it from the environment or a secret manager:

export ERP_URL="https://erp.example.com"
export ERP_KEY="your-api-key"
export ERP_SECRET="your-api-secret"

4. Make your first call#

import os

from frappex_client import Client, TokenAuth

auth = TokenAuth(os.environ["ERP_KEY"], os.environ["ERP_SECRET"])

with Client(os.environ["ERP_URL"], auth) as client:
    print(client.ping())  # "pong": the site answers
    print(client.get_logged_user())  # the user the key belongs to

Client is a context manager: the with block closes its connections when you are done. If you keep a client for the lifetime of a program, call client.close() on shutdown instead.

5. Read and write documents#

with Client(os.environ["ERP_URL"], auth) as client:
    todo = client.insert("ToDo", {"description": "Call the supplier"})
    print(todo["name"])  # e.g. "a1b2c3d4e5"

    client.update("ToDo", todo["name"], {"status": "Closed"})

    for row in client.iter_list("ToDo", fields=["name", "status"], filters={"status": "Open"}):
        print(row["name"], row["status"])

    client.delete("ToDo", todo["name"])

Every document comes back as a plain dict, exactly as the Frappe API sends it.

6. Handle errors#

from frappex_client import NotFoundError, PermissionDeniedError

try:
    client.get_doc("Customer", "Unknown Customer")
except NotFoundError:
    print("no such customer")
except PermissionDeniedError:
    print("this user may not read customers")

Every exception frappex-client raises derives from FrappexError. See Errors for the full list.

Local development sites#

frappex-client refuses plain http:// because credentials would travel unencrypted. For a site on your own machine, opt in explicitly:

client = Client("http://localhost:8000", auth, allow_insecure_http=True)

On a multi-site bench where several sites share one port, also send the site's name as the Host header. See Configuration.

Next steps#

  • Authentication: API keys, OAuth tokens, passwords and guest access.
  • Documents: everything about reading and writing records.
  • Async client: the same API for asyncio programs.

Found a mistake? Open an issue.