Skip to content

resources

Monzo API 'transactions' resource.

TransactionsResource dataclass

Bases: BaseResource

Monzo API 'transactions' resource.

Note

Monzo API docs: https://docs.monzo.com/#transactions

Source code in pymonzo/transactions/resources.py
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class TransactionsResource(BaseResource):
    """Monzo API 'transactions' resource.

    Note:
        Monzo API docs: https://docs.monzo.com/#transactions
    """

    def get(
        self,
        transaction_id: str,
        *,
        expand_merchant: bool = False,
    ) -> MonzoTransaction:
        """Return single transaction.

        Note:
            Monzo API docs: https://docs.monzo.com/#retrieve-transaction

        Arguments:
            transaction_id: The ID of the transaction.
            expand_merchant: Whether to return expanded merchant information.

        Returns:
            A Monzo transaction.
        """
        endpoint = f"/transactions/{transaction_id}"
        params = {}
        if expand_merchant:
            params["expand[]"] = "merchant"

        response = self._get_response(method="get", endpoint=endpoint, params=params)

        transaction = MonzoTransaction(**response.json()["transaction"])

        return transaction

    def annotate(
        self,
        transaction_id: str,
        metadata: Dict[str, str],
    ) -> MonzoTransaction:
        """Annotate transaction with extra metadata.

        Note:
            Monzo API docs: https://docs.monzo.com/#annotate-transaction

        Arguments:
            transaction_id: The ID of the transaction.
            metadata: Include each key you would like to modify. To delete a key,
                set its value to an empty string.

        Returns:
            Annotated Monzo transaction.
        """
        endpoint = f"/transactions/{transaction_id}"
        data = {f"metadata[{key}]": value for key, value in metadata.items()}

        response = self._get_response(method="patch", endpoint=endpoint, data=data)

        transaction = MonzoTransaction(**response.json()["transaction"])

        return transaction

    def list(
        self,
        account_id: Optional[str] = None,
        *,
        expand_merchant: bool = False,
        since: Optional[datetime] = None,
        before: Optional[datetime] = None,
        limit: Optional[int] = None,
    ) -> List[MonzoTransaction]:
        """Return a list of account transactions.

        You can only fetch all transactions within 5 minutes of authentication.
        After that, you can query your last 90 days.

        Note:
            Monzo API docs: https://docs.monzo.com/#list-transactions

        Arguments:
            account_id: The ID of the account. Can be omitted if user has only one
                active account.
            expand_merchant: Whether to return expanded merchant information.
            since: Filter transactions by start time.
            before: Filter transactions by end time.
            limit: Limits the number of results per-page. Maximum: 100.

        Returns:
            List of Monzo transactions.

        Raises:
            CannotDetermineDefaultAccount: If no account ID was passed and default
                account cannot be determined.
        """
        if not account_id:
            account_id = self.client.accounts.get_default_account().id

        endpoint = "/transactions"
        params = {"account_id": account_id}

        if expand_merchant:
            params["expand[]"] = "merchant"

        if since:
            params["since"] = since.strftime("%Y-%m-%dT%H:%M:%SZ")

        if before:
            params["before"] = before.strftime("%Y-%m-%dT%H:%M:%SZ")

        if limit:
            params["limit"] = str(limit)

        response = self._get_response(method="get", endpoint=endpoint, params=params)

        transactions = [
            MonzoTransaction(**transaction)
            for transaction in response.json()["transactions"]
        ]

        return transactions

annotate(transaction_id, metadata)

Annotate transaction with extra metadata.

Note

Monzo API docs: https://docs.monzo.com/#annotate-transaction

Parameters:

Name Type Description Default
transaction_id str

The ID of the transaction.

required
metadata Dict[str, str]

Include each key you would like to modify. To delete a key, set its value to an empty string.

required

Returns:

Type Description
MonzoTransaction

Annotated Monzo transaction.

Source code in pymonzo/transactions/resources.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def annotate(
    self,
    transaction_id: str,
    metadata: Dict[str, str],
) -> MonzoTransaction:
    """Annotate transaction with extra metadata.

    Note:
        Monzo API docs: https://docs.monzo.com/#annotate-transaction

    Arguments:
        transaction_id: The ID of the transaction.
        metadata: Include each key you would like to modify. To delete a key,
            set its value to an empty string.

    Returns:
        Annotated Monzo transaction.
    """
    endpoint = f"/transactions/{transaction_id}"
    data = {f"metadata[{key}]": value for key, value in metadata.items()}

    response = self._get_response(method="patch", endpoint=endpoint, data=data)

    transaction = MonzoTransaction(**response.json()["transaction"])

    return transaction

get(transaction_id, *, expand_merchant=False)

Return single transaction.

Note

Monzo API docs: https://docs.monzo.com/#retrieve-transaction

Parameters:

Name Type Description Default
transaction_id str

The ID of the transaction.

required
expand_merchant bool

Whether to return expanded merchant information.

False

Returns:

Type Description
MonzoTransaction

A Monzo transaction.

Source code in pymonzo/transactions/resources.py
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def get(
    self,
    transaction_id: str,
    *,
    expand_merchant: bool = False,
) -> MonzoTransaction:
    """Return single transaction.

    Note:
        Monzo API docs: https://docs.monzo.com/#retrieve-transaction

    Arguments:
        transaction_id: The ID of the transaction.
        expand_merchant: Whether to return expanded merchant information.

    Returns:
        A Monzo transaction.
    """
    endpoint = f"/transactions/{transaction_id}"
    params = {}
    if expand_merchant:
        params["expand[]"] = "merchant"

    response = self._get_response(method="get", endpoint=endpoint, params=params)

    transaction = MonzoTransaction(**response.json()["transaction"])

    return transaction

list(account_id=None, *, expand_merchant=False, since=None, before=None, limit=None)

Return a list of account transactions.

You can only fetch all transactions within 5 minutes of authentication. After that, you can query your last 90 days.

Note

Monzo API docs: https://docs.monzo.com/#list-transactions

Parameters:

Name Type Description Default
account_id Optional[str]

The ID of the account. Can be omitted if user has only one active account.

None
expand_merchant bool

Whether to return expanded merchant information.

False
since Optional[datetime]

Filter transactions by start time.

None
before Optional[datetime]

Filter transactions by end time.

None
limit Optional[int]

Limits the number of results per-page. Maximum: 100.

None

Returns:

Type Description
List[MonzoTransaction]

List of Monzo transactions.

Raises:

Type Description
CannotDetermineDefaultAccount

If no account ID was passed and default account cannot be determined.

Source code in pymonzo/transactions/resources.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def list(
    self,
    account_id: Optional[str] = None,
    *,
    expand_merchant: bool = False,
    since: Optional[datetime] = None,
    before: Optional[datetime] = None,
    limit: Optional[int] = None,
) -> List[MonzoTransaction]:
    """Return a list of account transactions.

    You can only fetch all transactions within 5 minutes of authentication.
    After that, you can query your last 90 days.

    Note:
        Monzo API docs: https://docs.monzo.com/#list-transactions

    Arguments:
        account_id: The ID of the account. Can be omitted if user has only one
            active account.
        expand_merchant: Whether to return expanded merchant information.
        since: Filter transactions by start time.
        before: Filter transactions by end time.
        limit: Limits the number of results per-page. Maximum: 100.

    Returns:
        List of Monzo transactions.

    Raises:
        CannotDetermineDefaultAccount: If no account ID was passed and default
            account cannot be determined.
    """
    if not account_id:
        account_id = self.client.accounts.get_default_account().id

    endpoint = "/transactions"
    params = {"account_id": account_id}

    if expand_merchant:
        params["expand[]"] = "merchant"

    if since:
        params["since"] = since.strftime("%Y-%m-%dT%H:%M:%SZ")

    if before:
        params["before"] = before.strftime("%Y-%m-%dT%H:%M:%SZ")

    if limit:
        params["limit"] = str(limit)

    response = self._get_response(method="get", endpoint=endpoint, params=params)

    transactions = [
        MonzoTransaction(**transaction)
        for transaction in response.json()["transactions"]
    ]

    return transactions