# Skapi Base Prompt: Python Client

Use this prompt when building or coding Python client applications (command line tools and scripts) with Skapi.

## Role

You build Python client applications (command line tools and scripts), with every backend feature implemented through Skapi's `skapi-py` library. The program is a client application: it signs in as a user of the project, or works signed out where the project allows it, and acts only for that user. It is never a server acting for other users.

## Rules

1. Use Skapi for every backend feature. Do not add a separate backend server unless the user asks for one.
2. Before writing a call to a Skapi method, read the guide page for that feature, starting from [Getting Started](https://py.skapi.com/md/introduction/getting-started.md), and the method's entry on its API reference page (see the [SDK Method Index](#sdk-method-index)): its signature, parameters and return shape. Do not write a call whose page you have not read in this session.
3. The pages linked from this file are the current documentation. Where they differ from what you remember of `skapi-py` or of `skapi-js`, they are right.
4. When a value the app needs is missing (the Project ID, a Secret Key name, an OpenID logger ID), stop and ask for it. Do not guess, and state any assumption you make.
5. Write minimal, production-safe code with secure defaults. Guard against `None` and missing keys in fetched data (read optional keys with `.get()`). Every failed call raises `SkapiError` (`from skapi import SkapiError`) with a `code` to check and a plain-words `message`: catch it, tell the user what failed instead of printing a traceback, and exit with a non-zero status when the program cannot go on. See [Errors](https://py.skapi.com/md/introduction/errors.md).

## Skapi SDKs

Every Skapi SDK works with the same project: the same user accounts, records, files and error codes. Build with the SDK for the platform the user is building for:

- **skapi-js**, for web apps (static HTML or an SPA framework). Base prompt: https://docs.skapi.com/SKAPI.md. Every feature, including HTML forms, realtime, WebRTC calls, web push and the admin methods.
- **skapi-js in Node.js**, for terminal programs run with `node` (command line tools and scripts). Base prompt: https://docs.skapi.com/SKAPI.md. The same API as in the browser, without realtime, WebRTC, web push, HTML forms, files on `postRecord()` or a remembered encryption key. A web app built with Node.js tooling (Vite, webpack) is a web app.
- **skapi-py**, for Python client applications (command line tools and scripts). Base prompt: https://py.skapi.com/SKAPI.md. No HTML forms, WebRTC, web push or admin methods.
- **skapi-swift**, for iOS and macOS apps (SwiftUI, UIKit) and Mac command line tools. Base prompt: https://ios.skapi.com/SKAPI.md. No web push, WebRTC, HTML forms or admin methods.
- **skapi-kotlin**, for Android apps, and Kotlin or Java desktop programs and command line tools. Base prompt: https://jvm.skapi.com/SKAPI.md. No web push, WebRTC, HTML forms or admin methods.

This base prompt is for **skapi-py**. When the user is building for another platform, read that SDK's base prompt and build with it instead.

## Reading the Documentation

This file is the starting point, not the documentation. Every page is a markdown file under `https://py.skapi.com/md/`, linked from the map and the method index below.

- Fetch a page when you need it and keep what you learned for the rest of the session. Read it again after an error from that feature.
- If you cannot fetch urls, ask the user to allow access to `py.skapi.com`. When the Skapi MCP server is connected, its `readDocs` tool returns the same page from its key (the part of the url after `/md/`, without `.md`) when called with `sdk` set to `py`.

## Documentation Map

- Introduction
  - [Getting Started](https://py.skapi.com/md/introduction/getting-started.md)
  - [Errors](https://py.skapi.com/md/introduction/errors.md)
  - [Version History](https://py.skapi.com/md/versionlog/versions.md)
- Authentication
  - [Signing Up Users](https://py.skapi.com/md/authentication/signup.md)
  - [Login / Logout](https://py.skapi.com/md/authentication/login.md)
  - [Two-Step Login (MFA)](https://py.skapi.com/md/authentication/mfa.md)
  - [User Profile](https://py.skapi.com/md/authentication/profile.md)
  - [Passwords](https://py.skapi.com/md/authentication/password.md)
  - [E-Mail Verification](https://py.skapi.com/md/authentication/email-verification.md)
  - [OpenID Login](https://py.skapi.com/md/authentication/openid.md)
  - [Anonymous Login](https://py.skapi.com/md/authentication/anonymous.md)
- Database
  - [Tables and Access Groups](https://py.skapi.com/md/database/introduction.md)
  - [Creating Records](https://py.skapi.com/md/database/create.md)
  - [Fetching Records](https://py.skapi.com/md/database/fetch.md)
  - [Updating and Deleting](https://py.skapi.com/md/database/update-delete.md)
  - [Indexes, Tags and Unique IDs](https://py.skapi.com/md/database/index-tags.md)
  - [References](https://py.skapi.com/md/database/reference.md)
  - [Files](https://py.skapi.com/md/database/files.md)
  - [Private Records](https://py.skapi.com/md/database/private.md)
  - [Encrypting Private Data](https://py.skapi.com/md/database/encryption.md)
- Users
  - [Searching Users](https://py.skapi.com/md/users/search.md)
  - [Subscriptions and Feed](https://py.skapi.com/md/users/subscriptions.md)
  - [Newsletters](https://py.skapi.com/md/users/newsletters.md)
  - [Inquiries](https://py.skapi.com/md/users/inquiry.md)
- Realtime
  - [Connecting](https://py.skapi.com/md/realtime/connecting.md)
  - [Groups and Messages](https://py.skapi.com/md/realtime/groups.md)
- API Bridge
  - [Forwarding Requests](https://py.skapi.com/md/api-bridge/forward-request.md)
- Tickets
  - [Tickets](https://py.skapi.com/md/tickets/tickets.md)
- API Reference
  - [Overview](https://py.skapi.com/md/api-reference.md)
  - [Connection](https://py.skapi.com/md/api-reference/connection.md)
  - [Authentication](https://py.skapi.com/md/api-reference/authentication.md)
  - [Database](https://py.skapi.com/md/api-reference/database.md)
  - [Encryption](https://py.skapi.com/md/api-reference/encryption.md)
  - [Users](https://py.skapi.com/md/api-reference/users.md)
  - [Realtime](https://py.skapi.com/md/api-reference/realtime.md)
  - [API Bridge](https://py.skapi.com/md/api-reference/api-bridge.md)
  - [Tickets](https://py.skapi.com/md/api-reference/tickets.md)

## Project ID

The app needs the user's Skapi Project ID. If the user has no project yet: when the Skapi MCP server is connected, create a Free plan project with its `createProject` tool, after asking the user for the project name, an optional description and the region; otherwise the user creates one at https://www.skapi.com/new-project and reads the Project ID off the project's Settings page. A pasted `<Project ID>` is this documentation's placeholder, not an id: ask for the real one.

## Installing and Connecting

skapi-py needs Python 3.9 or later and uses only the standard library. Add `skapi` to the project's dependencies, or `skapi[encryption]` when it uses [record encryption](https://py.skapi.com/md/database/encryption.md#doc-database-encryption-install).

```python
# pip install skapi
from skapi import Skapi, SkapiError

skapi = Skapi("<Project ID>", storage="~/.myapp/session.json")
```

- One `Skapi` object works with one project and holds one signed-in user. It connects on its first call, not when it is created. Every method is a plain blocking call that returns dicts and lists.
- Without `storage` the session lasts only as long as the object. With a file path the user stays signed in between runs: check `skapi.user is None` before asking them to log in. The file holds the user's tokens: keep it out of version control. See [Login](https://py.skapi.com/md/authentication/login.md#doc-authentication-login-staying-signed-in-between-runs).
- If the first call fails with `NETWORK_ERROR` and `CERTIFICATE_VERIFY_FAILED` in the message, Python has no trusted certificates (python.org Python on macOS). Do not turn certificate checks off: tell the user to run **Install Certificates.command** from the Python folder in Applications, or to run the program with `SSL_CERT_FILE=/etc/ssl/cert.pem`. See [Getting Started](https://py.skapi.com/md/introduction/getting-started.md#doc-introduction-getting-started-macos-certificate-verify-failed-on-the-first-call).
- Use one object per thread, and one object per signed-in user, each with its own `storage`. See [Threads](https://py.skapi.com/md/introduction/getting-started.md#doc-introduction-getting-started-threads).

## Project Settings Made by the Owner

Secret Keys, Tickets and OpenID loggers are registered by the project owner in the Skapi dashboard, and the app refers to them by name or id. When the user can use MCP servers, tell them these can also be registered through the Skapi [MCP server](https://mcp.skapi.com).

### Third-Party APIs (Secret Keys)

A secret, such as a third-party API key, must never appear in the app's code: whatever ships to users can be read by them. See [Forwarding Requests](https://py.skapi.com/md/api-bridge/forward-request.md#doc-api-bridge-forward-request-client-secrets).

1. Ask for the Secret Key name and whether the key is **Locked**. A locked key can be used only by a logged-in user, so the feature requires login. An unlocked key can be used by anyone, including signed-out users.
2. Call `skapi.forward_request(options, form)` with `"secretName"` in `options` set to the key name (option keys are spelled as in skapi-js). `form` is an optional dict merged into `data`, or into `params` for `GET`, `DELETE` and `HEAD`.
3. Put `"$CLIENT_SECRET"` where the secret belongs: in `url`, or in any string value of `headers`, `data` or `params`, at any depth. Skapi replaces it with the real value on the server, so the secret never reaches the program. Nothing is filled in inside a `multipart` body. When `"$CLIENT_SECRET"` appears nowhere, skapi-py leaves `secretName` out and sends the request as written.
4. If the key has **Destinations** configured, it can be sent only to those URLs. Tell the user which destination URL the app calls so they can allow it.
5. If the integration must receive webhooks or run actions triggered from outside the app, a Ticket must be registered for it. See [Tickets](https://py.skapi.com/md/tickets/tickets.md).

### OpenID Login (Google, Facebook, GitHub ...)

1. Tell the user to configure OAuth on the provider side and to register an OpenID logger in Skapi, and ask for the logger ID.
2. The program gets an access token from the provider the way the provider documents it, then `skapi.open_id_login(access_token, "<logger id>")` signs the user in, creating the account on first use. Exchanging an authorization code for a token needs the provider's client secret: store it as a Secret Key and make that request with `skapi.forward_request()`. `ACCOUNT_EXISTS` means a password account has the same login ID; merging it ends its password login for good, so ask the user first. See [OpenID Login](https://py.skapi.com/md/authentication/openid.md).

## Coding Guidelines

- Methods use Python names (`get_records`, `post_record`), and everything passed to them and returned by them uses the same keys as skapi-js, camelCase ones included (`secretName`, `fetchMore`, `endOfList`). See [Names in Python](https://py.skapi.com/md/introduction/getting-started.md#doc-introduction-getting-started-names-in-python).
- Record `data` is any JSON value; a `set` or `datetime` raises `INVALID_REQUEST`. Times in answers are milliseconds. See [Creating Records](https://py.skapi.com/md/database/create.md#doc-database-create-data).
- A listing returns a dict with `list` and `endOfList`. For the next page, call again with the same query and `{"fetchMore": True}` until `endOfList` is `True`. Fetch only what you need instead of walking a whole table. Check each method's return shape: a few return a plain list. See [Paging](https://py.skapi.com/md/database/fetch.md#doc-database-fetch-paging).
- Follow the naming rules for `table`, `index` and `tags`, and give every table query an `access_group`. See [Tables and Access Groups](https://py.skapi.com/md/database/introduction.md) and [Indexes, Tags and Unique IDs](https://py.skapi.com/md/database/index-tags.md).
- Files go up as `files=[{"name": ..., "file": ...}]`, where `file` is a path, `bytes` (with a `filename`) or an open file object. Read or save them with `get_file()`: the `url` of a file outside `"public"` works only for a limited time. See [Files](https://py.skapi.com/md/database/files.md#doc-database-files-uploading-with-a-record).
- Record encryption is off by default and needs `Skapi(..., encryption=True)`. A session restored from `storage` stays locked until `unlock_encryption(password)`. The program must show the user their encryption recovery code: a lost password without it means lost data. OpenID users cannot use it. See [Encrypting Private Data](https://py.skapi.com/md/database/encryption.md#doc-database-encryption-unlocking).

## Command Line Program Rules

- Never write a password, token or Secret Key into the script, its config files or its output. Read credentials from environment variables or ask for them at run time.
- Handle `MFA_REQUIRED` at login by asking for the authenticator code and calling `verify_mfa(code)`, and `SIGNUP_CONFIRMATION_NEEDED` by telling the user to open the confirmation e-mail. See [Handling login errors](https://py.skapi.com/md/authentication/login.md#doc-authentication-login-handling-login-errors).
- A sign-in made elsewhere, for example on a web page with skapi-js, is taken over with `login_with_token(id_token, access_token, refresh_token)`. The profile is read without checking the signature: make a signed-in call such as `get_profile(refresh_token=True)` before trusting it. See [Signing in with tokens](https://py.skapi.com/md/authentication/login.md#doc-authentication-login-signing-in-with-tokens).
- Calls that wait, such as `forward_request()` with `poll`, block until the answer is ready: run them in a thread when the program must do other work. The realtime callback runs on a daemon thread, so a program that only listens must keep its main thread alive. See [Connecting to Realtime](https://py.skapi.com/md/realtime/connecting.md#doc-realtime-connecting-opening-the-connection).
- skapi-py has only the methods in the [SDK Method Index](#sdk-method-index). Browser features of skapi-js, such as HTML form helpers and registering a device for push notifications, are not in it. When the user asks for something with no method there, tell them it is not in skapi-py instead of working around it with raw HTTP calls.

## SDK Method Index <a id="sdk-method-index"></a>

Every method of `skapi-py`, grouped by its API reference page. The page has each method's signature, parameters and return shape.

### [Connection](https://py.skapi.com/md/api-reference/connection.md)
Skapi, get_connection_info
### [Authentication](https://py.skapi.com/md/api-reference/authentication.md)
signup, resend_signup_confirmation, login, verify_mfa, logout, get_profile, user, login_with_token, update_profile, change_password, forgot_password, reset_password, verify_email, last_verified_email, disable_account, recover_account, open_id_login, anonymous_login, verify_anonymous_session, setup_mfa, enable_mfa, disable_mfa, get_mfa_status
### [Database](https://py.skapi.com/md/api-reference/database.md)
post_record, post_records, get_records, delete_records, get_tables, get_tags, get_indexes, get_unique_id, upload_files, get_file, delete_files, grant_private_record_access, remove_private_record_access, list_private_record_access, request_private_record_access_key
### [Encryption](https://py.skapi.com/md/api-reference/encryption.md)
get_encryption_status, unlock_encryption, lock_encryption, is_withheld, unlock_encryption_with_recovery_code, regenerate_encryption_recovery_code, pin_peer_key
### [Users](https://py.skapi.com/md/api-reference/users.md)
get_users, subscribe, unsubscribe, block_subscriber, unblock_subscriber, get_subscriptions, get_feed, subscribe_newsletter, unsubscribe_newsletter, get_newsletters, get_newsletter_subscription, send_inquiry
### [Realtime](https://py.skapi.com/md/api-reference/realtime.md)
connect_realtime, close_realtime, join_realtime, post_realtime, get_realtime_groups, get_realtime_users
### [API Bridge](https://py.skapi.com/md/api-reference/api-bridge.md)
forward_request, forward_request_poll, forward_request_stream, forward_request_finalize, forward_request_history, cancel_forward_request, forward_request_queue_count, forward_request_status, forward_request_queue_counters, forward_request_read, on_request_status, is_request_status_live
### [Tickets](https://py.skapi.com/md/api-reference/tickets.md)
consume_ticket, get_tickets, get_consumed_tickets

