Skip to content

resources

Monzo API 'pots' resource.

PotsResource dataclass

Bases: BaseResource

Monzo API 'pots' resource.

Note

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

Source code in pymonzo/pots/resources.py
 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
@dataclass
class PotsResource(BaseResource):
    """Monzo API 'pots' resource.

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

    _cached_pots: Dict[str, List[MonzoPot]] = field(default_factory=dict)

    def get_default_pot(self, account_id: Optional[str] = None) -> MonzoPot:
        """If the user has only one (active) pot, treat it as the default pot.

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

        Returns:
            User's active pot.

        Raises:
            CannotDetermineDefaultPot: If user has more than one active pot.
            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

        pots = self.list(account_id)

        # If there is only one pot, return it
        if len(pots) == 1:
            return pots[0]

        # Otherwise check if there is only one active (non-deleted) pot
        active_pots = [pot for pot in pots if not pot.deleted]

        if len(active_pots) == 1:
            return active_pots[0]

        raise CannotDetermineDefaultPot(
            "Cannot determine default pot. "
            "You need to explicitly pass an 'pot_id' argument."
        )

    def list(
        self,
        account_id: Optional[str] = None,
        *,
        refresh: bool = False,
    ) -> List[MonzoPot]:
        """Return a list of user's pots.

        It's often used when deciding whether to require explicit pot ID
        or use the only active one, so we cache the response by default.

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

        Arguments:
            account_id: The ID of the account. Can be omitted if user has only one
                active account.
            refresh: Whether to refresh the cached list of pots.

        Returns:
            A list of user's pots.

        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

        if not refresh and self._cached_pots.get(account_id):
            return self._cached_pots[account_id]

        endpoint = "/pots"
        params = {"current_account_id": account_id}
        response = self._get_response(method="get", endpoint=endpoint, params=params)

        pots = [MonzoPot(**pot) for pot in response.json()["pots"]]
        self._cached_pots[account_id] = pots

        return pots

    def deposit(
        self,
        amount: Union[int, float],
        pot_id: Optional[str] = None,
        *,
        account_id: Optional[str] = None,
        dedupe_id: Optional[str] = None,
    ) -> MonzoPot:
        """Move money from an account to a pot.

        Note:
            Monzo API docs: https://docs.monzo.com/#deposit-into-a-pot

        Arguments:
            amount: The amount to deposit, as a 64bit integer in minor units of
                the currency, eg. pennies for GBP, or cents for EUR and USD.
            pot_id: The ID of the pot to deposit into.
            account_id: The ID of the account to withdraw from. Can be omitted if
                user has only one active account.
            dedupe_id: A unique string used to de-duplicate deposits. Ensure this
                remains static between retries to ensure only one deposit is created.
                If omitted, a random 16 character string will be generated.

        Returns:
            A Monzo pot.

        Raises:
            CannotDetermineDefaultPot: If user has more than one active pot.
            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

        if not pot_id:
            pot_id = self.get_default_pot(account_id).id

        if not dedupe_id:
            dedupe_id = token_urlsafe(16)

        endpoint = f"/pots/{pot_id}/deposit"
        data = {
            "source_account_id": account_id,
            "amount": amount,
            "dedupe_id": dedupe_id,
        }
        response = self._get_response(method="put", endpoint=endpoint, data=data)

        pot = MonzoPot(**response.json())

        return pot

    def withdraw(
        self,
        amount: Union[int, float],
        pot_id: Optional[str] = None,
        *,
        account_id: Optional[str] = None,
        dedupe_id: Optional[str] = None,
    ) -> MonzoPot:
        """Withdraw money from a pot to an account.

        Note:
            Monzo API docs: https://docs.monzo.com/#withdraw-from-a-pot

        Arguments:
            amount: The amount to deposit, as a 64bit integer in minor units of
                the currency, eg. pennies for GBP, or cents for EUR and USD.
            pot_id: The ID of the pot to withdraw from.
            account_id: The ID of the account to deposit into. Can be omitted if
                user has only one active account.
            dedupe_id: A unique string used to de-duplicate deposits. Ensure this
                remains static between retries to ensure only one deposit is created.
                If omitted, a random 16 character string will be generated.

        Returns:
            A Monzo pot.

        Raises:
            CannotDetermineDefaultPot: If user has more than one active pot.
            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

        if not pot_id:
            pot_id = self.get_default_pot(account_id).id

        if not dedupe_id:
            dedupe_id = token_urlsafe(16)

        endpoint = f"/pots/{pot_id}/withdraw"
        data = {
            "destination_account_id": account_id,
            "amount": amount,
            "dedupe_id": dedupe_id,
        }
        response = self._get_response(method="put", endpoint=endpoint, data=data)

        pot = MonzoPot(**response.json())

        return pot

deposit(amount, pot_id=None, *, account_id=None, dedupe_id=None)

Move money from an account to a pot.

Note

Monzo API docs: https://docs.monzo.com/#deposit-into-a-pot

Parameters:

Name Type Description Default
amount Union[int, float]

The amount to deposit, as a 64bit integer in minor units of the currency, eg. pennies for GBP, or cents for EUR and USD.

required
pot_id Optional[str]

The ID of the pot to deposit into.

None
account_id Optional[str]

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

None
dedupe_id Optional[str]

A unique string used to de-duplicate deposits. Ensure this remains static between retries to ensure only one deposit is created. If omitted, a random 16 character string will be generated.

None

Returns:

Type Description
MonzoPot

A Monzo pot.

Raises:

Type Description
CannotDetermineDefaultPot

If user has more than one active pot.

CannotDetermineDefaultAccount

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

Source code in pymonzo/pots/resources.py
 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def deposit(
    self,
    amount: Union[int, float],
    pot_id: Optional[str] = None,
    *,
    account_id: Optional[str] = None,
    dedupe_id: Optional[str] = None,
) -> MonzoPot:
    """Move money from an account to a pot.

    Note:
        Monzo API docs: https://docs.monzo.com/#deposit-into-a-pot

    Arguments:
        amount: The amount to deposit, as a 64bit integer in minor units of
            the currency, eg. pennies for GBP, or cents for EUR and USD.
        pot_id: The ID of the pot to deposit into.
        account_id: The ID of the account to withdraw from. Can be omitted if
            user has only one active account.
        dedupe_id: A unique string used to de-duplicate deposits. Ensure this
            remains static between retries to ensure only one deposit is created.
            If omitted, a random 16 character string will be generated.

    Returns:
        A Monzo pot.

    Raises:
        CannotDetermineDefaultPot: If user has more than one active pot.
        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

    if not pot_id:
        pot_id = self.get_default_pot(account_id).id

    if not dedupe_id:
        dedupe_id = token_urlsafe(16)

    endpoint = f"/pots/{pot_id}/deposit"
    data = {
        "source_account_id": account_id,
        "amount": amount,
        "dedupe_id": dedupe_id,
    }
    response = self._get_response(method="put", endpoint=endpoint, data=data)

    pot = MonzoPot(**response.json())

    return pot

get_default_pot(account_id=None)

If the user has only one (active) pot, treat it as the default pot.

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

Returns:

Type Description
MonzoPot

User's active pot.

Raises:

Type Description
CannotDetermineDefaultPot

If user has more than one active pot.

CannotDetermineDefaultAccount

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

Source code in pymonzo/pots/resources.py
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
def get_default_pot(self, account_id: Optional[str] = None) -> MonzoPot:
    """If the user has only one (active) pot, treat it as the default pot.

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

    Returns:
        User's active pot.

    Raises:
        CannotDetermineDefaultPot: If user has more than one active pot.
        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

    pots = self.list(account_id)

    # If there is only one pot, return it
    if len(pots) == 1:
        return pots[0]

    # Otherwise check if there is only one active (non-deleted) pot
    active_pots = [pot for pot in pots if not pot.deleted]

    if len(active_pots) == 1:
        return active_pots[0]

    raise CannotDetermineDefaultPot(
        "Cannot determine default pot. "
        "You need to explicitly pass an 'pot_id' argument."
    )

list(account_id=None, *, refresh=False)

Return a list of user's pots.

It's often used when deciding whether to require explicit pot ID or use the only active one, so we cache the response by default.

Note

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

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
refresh bool

Whether to refresh the cached list of pots.

False

Returns:

Type Description
List[MonzoPot]

A list of user's pots.

Raises:

Type Description
CannotDetermineDefaultAccount

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

Source code in pymonzo/pots/resources.py
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
def list(
    self,
    account_id: Optional[str] = None,
    *,
    refresh: bool = False,
) -> List[MonzoPot]:
    """Return a list of user's pots.

    It's often used when deciding whether to require explicit pot ID
    or use the only active one, so we cache the response by default.

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

    Arguments:
        account_id: The ID of the account. Can be omitted if user has only one
            active account.
        refresh: Whether to refresh the cached list of pots.

    Returns:
        A list of user's pots.

    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

    if not refresh and self._cached_pots.get(account_id):
        return self._cached_pots[account_id]

    endpoint = "/pots"
    params = {"current_account_id": account_id}
    response = self._get_response(method="get", endpoint=endpoint, params=params)

    pots = [MonzoPot(**pot) for pot in response.json()["pots"]]
    self._cached_pots[account_id] = pots

    return pots

withdraw(amount, pot_id=None, *, account_id=None, dedupe_id=None)

Withdraw money from a pot to an account.

Note

Monzo API docs: https://docs.monzo.com/#withdraw-from-a-pot

Parameters:

Name Type Description Default
amount Union[int, float]

The amount to deposit, as a 64bit integer in minor units of the currency, eg. pennies for GBP, or cents for EUR and USD.

required
pot_id Optional[str]

The ID of the pot to withdraw from.

None
account_id Optional[str]

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

None
dedupe_id Optional[str]

A unique string used to de-duplicate deposits. Ensure this remains static between retries to ensure only one deposit is created. If omitted, a random 16 character string will be generated.

None

Returns:

Type Description
MonzoPot

A Monzo pot.

Raises:

Type Description
CannotDetermineDefaultPot

If user has more than one active pot.

CannotDetermineDefaultAccount

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

Source code in pymonzo/pots/resources.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def withdraw(
    self,
    amount: Union[int, float],
    pot_id: Optional[str] = None,
    *,
    account_id: Optional[str] = None,
    dedupe_id: Optional[str] = None,
) -> MonzoPot:
    """Withdraw money from a pot to an account.

    Note:
        Monzo API docs: https://docs.monzo.com/#withdraw-from-a-pot

    Arguments:
        amount: The amount to deposit, as a 64bit integer in minor units of
            the currency, eg. pennies for GBP, or cents for EUR and USD.
        pot_id: The ID of the pot to withdraw from.
        account_id: The ID of the account to deposit into. Can be omitted if
            user has only one active account.
        dedupe_id: A unique string used to de-duplicate deposits. Ensure this
            remains static between retries to ensure only one deposit is created.
            If omitted, a random 16 character string will be generated.

    Returns:
        A Monzo pot.

    Raises:
        CannotDetermineDefaultPot: If user has more than one active pot.
        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

    if not pot_id:
        pot_id = self.get_default_pot(account_id).id

    if not dedupe_id:
        dedupe_id = token_urlsafe(16)

    endpoint = f"/pots/{pot_id}/withdraw"
    data = {
        "destination_account_id": account_id,
        "amount": amount,
        "dedupe_id": dedupe_id,
    }
    response = self._get_response(method="put", endpoint=endpoint, data=data)

    pot = MonzoPot(**response.json())

    return pot