Actual
Actual
Actual(
base_url: str = "http://localhost:5006",
token: str | None = None,
password: str | None = None,
file: str | None = None,
encryption_password: str | None = None,
data_dir: str | Path | None = None,
cert: bool | SSLContext | str = True,
bootstrap: bool = False,
sa_kwargs: dict | None = None,
extra_headers: dict[str, str] | None = None,
timeout: float | Timeout | None = 60.0,
)
Bases: ActualServer
Implements the Python API for the Actual Server to be able to read and modify information on Actual using Python.
Parts of the implementation are available at the following file.
The client is expected to be used as a context manager, just like the following:
from actual import Actual
from actual.queries import get_transactions
with Actual(
# Url of the Actual Server
base_url="http://localhost:5006",
# Password for authentication
password="<your_password>",
# Set the file to work with.
# Can be either the file id or filename (if the name is unique)
file="<file_id_or_name>",
# Optional: Password for the file encryption.
# Will not use it if set to None.
encryption_password=None,
# Optional: Directory to store downloaded files.
# Will use a temporary if not provided
data_dir="<path_to_data_directory>",
# Optional: Path to the certificate file to use for the connection.
# Can be set to `False` to disable SSL verification
cert="<path_to_cert_file>"
) as actual:
transactions = get_transactions(actual.session)
Parameters:
-
(base_urlstr, default:'http://localhost:5006') –Url of the running Actual server
-
(tokenstr | None, default:None) –The token for authentication, if this is available (optional)
-
(passwordstr | None, default:None) –The password for authentication. It will be used on the .login() method to retrieve the token.
-
(filestr | None, default:None) –The name or id of the file to be set
-
(encryption_passwordstr | None, default:None) –Password used to configure encryption, if existing. Will raise an exception if it is not set but is required.
-
(data_dirstr | Path | None, default:None) –Path where to store the downloaded files from the server. If not specified, a temporary folder will be created instead. If database files are already present on the path, the library will try to reuse them by re-computing the sync request. Providing a path should speed up the download process considerably on the next call.
-
(certbool | SSLContext | str, default:True) –If a custom certificate should be used (e.g., self-signed certificate), its path can be provided as a string or as custom ssl.SSLContext. Set to
Falsefor no certificate check. -
(bootstrapbool, default:False) –If the server is not bootstrapped, bootstrap it with the password.
-
(sa_kwargsdict | None, default:None) –Additional
kwargspassed to the SQLAlchemy session maker. Examples areautoflush(enabled by default),autocommit(disabled by default). For a list of all parameters, check the SQLAlchemy documentation. -
(extra_headersdict[str, str] | None, default:None) –Additional headers to be attached to each request to the Actual server.
-
(timeoutfloat | Timeout | None, default:60.0) –Timeout in seconds applied to all HTTP requests. Set to
Noneto disable. Accepts a float or anhttpx.Timeoutobject for fine-grained control. Defaults to 60 seconds.
Methods:
-
set_file–Sets the file id for the class for further requests.
-
run_migrations–Runs the migration files, skipping the ones that have already been run.
-
create_budget–Creates a budget using the remote server default database and migrations.
-
rename_budget–Renames the budget with the given name.
-
delete_budget–Deletes the currently loaded file from the server.
-
cleanup–Cleans up the database from all deleted transactions, message caches and runs a
VACUUM. -
export_data–Export your data as a zip file containing db.sqlite and metadata.json files.
-
encrypt–Encrypts the local database using a new key, and re-uploads to the server.
-
upload_budget–Uploads the current file to the Actual server.
-
reupload_budget–Resets the user file on the backend and re-uploads the current copy instead.
-
apply_changes–Applies a list of sync changes, based on what the sync method returned on the remote.
-
get_metadata–Gets the content of the
metadata.jsonfile. -
update_metadata–Updates the
metadata.jsonfrom the Actual file with the patch fields. -
download_budget–Downloads the budget file from the remote, applying all following changes required by the server.
-
download_master_encryption_key–Downloads and assembles the key for decrypting the budget based on the provided encryption password.
-
import_zip–Imports a zip file as the current database, as well as generating the local reflected session.
-
create_engine–Internally creates the engine for the database, and loads the reflected metadata.
-
sync–Does a sync request and applies all changes that are stored on the server on the local copy of the database.
-
commit–Adds all pending entries done to the local database, and sends a sync request to the remote server.
-
run_rules–Runs all the stored rules on the database on all transactions, without any filters.
-
run_bank_sync–Runs the bank synchronization for the selected account. If missing, all accounts are synchronized.
Attributes:
-
file(RemoteFileListDTO) –Returns the current file. Raises if no file is set.
-
data_dir(Path) –Returns the data directory. Raises if not set.
-
session(Session) –Returns a session for using with the queries.
Source code in actual/__init__.py
data_dir
property
data_dir: Path
Returns the data directory. Raises if not set.
If the data_dir is not provided on budget creation, the default data directory will be created using a
temporary directory using the fileId as primary key.
set_file
set_file(
file_id: str | RemoteFileListDTO,
) -> RemoteFileListDTO
Sets the file id for the class for further requests.
The file_id argument can be either the name, the remote id or the group id (also known as sync_id) from the
file. If there are duplicates for the name, this method will raise UnknownFileId.
Source code in actual/__init__.py
run_migrations
Runs the migration files, skipping the ones that have already been run.
The files can be retrieved from data_file_index method. This first file is
the base database, and the following files are migrations. Migrations can also be .js files. In this case,
we have to extract and execute queries from the standard JS.
Source code in actual/__init__.py
create_budget
create_budget(budget_name: str)
Creates a budget using the remote server default database and migrations.
If a password is provided, the budget will be encrypted. It's important to note that create_budget
depends on the migration files from the Actual server, and those could be written in Javascript. Even though
the library tries to execute all statements in those files, it is not an exact match. It is recommended
to create budgets via frontend instead.
Source code in actual/__init__.py
rename_budget
rename_budget(budget_name: str)
delete_budget
Deletes the currently loaded file from the server.
cleanup
Cleans up the database from all deleted transactions, message caches and runs a VACUUM.
Useful to reduce the size of the database before exporting it.
Taken from source code at [actual/packages/loot-core/src/server/sync/reset.ts] (https://github.com/actualbudget/actual/blob/89006275a092d2309ab03162a047e07663789198/packages/loot-core/src/server/sync/reset.ts#L37-L47)
Source code in actual/__init__.py
export_data
export_data(
output_file: (
str | PathLike[str] | IO[bytes] | None
) = None,
cleanup: bool = True,
) -> bytes
Export your data as a zip file containing db.sqlite and metadata.json files.
It can be imported into another Actual instance by closing an open file (if any), then clicking the “Import file” button, then choosing “Actual”. Even when encryption is enabled, the exported zip file will not have any encryption.
The export will clean up the budget (i.e., remove up all changeset objects and entries marked for deletion), then writing a zip file to disk. See Actual.cleanup method for more information.
Source code in actual/__init__.py
encrypt
encrypt(encryption_password: str)
Encrypts the local database using a new key, and re-uploads to the server.
WARNING: this resets the file on the server. Make sure you have a copy of the database before attempting this operation.
Source code in actual/__init__.py
upload_budget
Uploads the current file to the Actual server.
If attempting to upload your first budget, make sure you use Actual.create_budget first.
Source code in actual/__init__.py
reupload_budget
Resets the user file on the backend and re-uploads the current copy instead.
Works similar to the reset sync option from the frontend.
This operation can be destructive, so make sure you generate a copy before attempting to re-upload your budget.
Source code in actual/__init__.py
apply_changes
Applies a list of sync changes, based on what the sync method returned on the remote.
Source code in actual/__init__.py
get_metadata
get_metadata() -> dict
update_metadata
update_metadata(patch: dict)
Updates the metadata.json from the Actual file with the patch fields.
The patch is a dictionary that will then be merged on the metadata and written again to a file.
Source code in actual/__init__.py
download_budget
download_budget(encryption_password: str | None = None)
Downloads the budget file from the remote, applying all following changes required by the server.
After the file is downloaded, the sync endpoint queries for the list of pending changes. The changes are individual row updates that are then applied one by one to the downloaded database state. See the sync method for more information.
If the budget is password-protected, the password needs to be present to download the budget. Otherwise, the budget download will fail with ActualDecryptionError.
When a data_dir was provided, the method will try to use the local downloaded copy by first checking if the
sync id (named group id) remains the same. If it does, then the sync is executed using the stored files.
Otherwise, the file is re-downloaded.
Source code in actual/__init__.py
download_master_encryption_key
Downloads and assembles the key for decrypting the budget based on the provided encryption password.
If the user file is not encryption, no key will be returned. If the file was encrypted, the key is assembled using the key salt and the password with the PBKDF2HMAC algorithm.
Source code in actual/__init__.py
import_zip
Imports a zip file as the current database, as well as generating the local reflected session.
This function enables you to inspect backups by loading them directly, instead of unzipping the contents.
Source code in actual/__init__.py
create_engine
Internally creates the engine for the database, and loads the reflected metadata.
Source code in actual/__init__.py
sync
Does a sync request and applies all changes that are stored on the server on the local copy of the database.
Returns a list of changes that were applied to the local database, so you can also use this method to retrieve all changes that were made to the budget. See Changeset for more information.
Source code in actual/__init__.py
commit
Adds all pending entries done to the local database, and sends a sync request to the remote server.
Only after a commit operation will the remote frontend show the new data.
It's important to note that this process is not atomic, so if the process is interrupted before it completes successfully, the files would end up in a unknown state, leading you to have to redo the budget download.
Source code in actual/__init__.py
run_rules
run_rules(
transactions: Sequence[Transactions] | None = None,
)
Runs all the stored rules on the database on all transactions, without any filters.
Source code in actual/__init__.py
run_bank_sync
run_bank_sync(
account: str | Accounts | None = None,
start_date: date | None = None,
run_rules: bool = False,
) -> list[Transactions]
Runs the bank synchronization for the selected account. If missing, all accounts are synchronized.
If a start_date is provided, is used as a reference; otherwise, the last timestamp of each account will be
used. If the account does not have any transaction, the last 90 days are considered instead.
If the start_date is not provided and the account does not have any transaction, a reconciled transaction will
be generated to match the expected balance of the account. This would correct the account balance with the
remote one.
If run_rules is set, the rules will be run for the imported transactions. Please note that unlike Actual,
the rules here are run at the final imported objects. This is unlikely to cause data mismatches,
but if you find any issue, feel free to report it on the Github repository.