frappex-client v0.1.1

Guides

Authentication#

A client authenticates in one of four ways. Pick one by passing an auth object as the second argument, or pass nothing for guest access.

Auth Header or mechanism Use it for
TokenAuth(api_key, api_secret) Authorization: token key:secret Servers, scripts and jobs. Recommended.
BearerAuth(token) Authorization: Bearer … An OAuth 2 access token issued by the site.
PasswordAuth(username, password) Session cookie Tools that act as a person who has no API key.
(none) Nothing is sent Health checks, public files, allow_guest methods.

API key and secret#

import os

from frappex_client import Client, TokenAuth

auth = TokenAuth(os.environ["ERP_KEY"], os.environ["ERP_SECRET"])
client = Client("https://erp.example.com", auth)

Every request carries the key pair. There is no login round trip, and the keys can be revoked from the user record without changing anyone's password. Getting started shows how to create them.

OAuth bearer token#

If your site is set up as an OAuth provider (OAuth Client in the desk), an app can obtain an access token through the OAuth flow and use it directly:

from frappex_client import BearerAuth, Client

client = Client("https://erp.example.com", BearerAuth(access_token))

frappex-client does not run the OAuth flow or refresh tokens. When a token expires, requests raise AuthenticationError; create a new client with a fresh token.

Username and password#

from frappex_client import Client, PasswordAuth

with Client("https://erp.example.com", PasswordAuth("jane@example.com", password)) as client:
    print(client.get_logged_user())  # logs in on this first request
# leaving the block logs out and closes the connection

The client logs in once, on its first request, and keeps the session cookie. Concurrent first requests share a single login. Closing the client logs out.

Warning

Prefer API keys for unattended programs. A password usually belongs to a person, may be protected by two-factor login, and cannot be revoked without changing it.

Guest access#

Leave out the auth argument and no credentials are sent at all:

from frappex_client import Client

with Client("https://erp.example.com") as guest:
    print(guest.ping())  # "pong"
    data = guest.download("/files/logo.png")  # public file

A guest can reach only what the site opens to everyone: ping, public files, and methods whitelisted with allow_guest=True. Anything else raises PermissionDeniedError (or AuthenticationError on some versions).

Keeping credentials secret#

The secret is wrapped in a Secret object as soon as it enters the library:

from frappex_client import TokenAuth

auth = TokenAuth("my-key", "my-very-secret-value")
print(auth)  # TokenAuth(api_key='my-key')
print(repr(client))  # Client(base_url='https://erp.example.com', auth=TokenAuth)

The value never appears in repr, str, log lines or exception messages. frappex-client also refuses credentials that contain line breaks, which would otherwise allow header injection.

Do

  • Read secrets from environment variables or a secret manager.
  • Use one dedicated integration user per program, with the fewest roles it needs.
  • Rotate keys when people with access leave.

Don't

  • Commit keys to git, even in private repositories.
  • Use an Administrator key for routine integrations.
  • Put credentials in the URL: https://user:pw@host is refused.

Found a mistake? Open an issue.