Skip to content

Hide My Email

Bases: BaseService

The 'Hide My Email' iCloud service connects to iCloud and manages email aliases.

This service allows users to: - Generate new email aliases - Reserve specific aliases - List all existing aliases - Get alias details by ID - Update alias metadata (label, note) - Delete aliases - Deactivate aliases - Reactivate aliases

Source code in pyicloud/services/hidemyemail.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
201
202
203
204
205
206
207
208
209
210
211
212
class HideMyEmailService(BaseService):
    """
    The 'Hide My Email' iCloud service connects to iCloud and manages email aliases.

    This service allows users to:
    - Generate new email aliases
    - Reserve specific aliases
    - List all existing aliases
    - Get alias details by ID
    - Update alias metadata (label, note)
    - Delete aliases
    - Deactivate aliases
    - Reactivate aliases
    """

    def __init__(
        self, service_root: str, session: PyiCloudSession, params: dict[str, Any]
    ) -> None:
        super().__init__(service_root, session, params)

        # Define v1 endpoints
        self._v1_endpoint: str = f"{service_root}/v1/hme"
        self._generate_endpoint: str = f"{self._v1_endpoint}/generate"
        self._reserve_endpoint: str = f"{self._v1_endpoint}/reserve"
        self._update_metadata_endpoint: str = f"{self._v1_endpoint}/updateMetaData"
        self._delete_endpoint: str = f"{self._v1_endpoint}/delete"
        self._deactivate_endpoint: str = f"{self._v1_endpoint}/deactivate"
        self._reactivate_endpoint: str = f"{self._v1_endpoint}/reactivate"

        # Define v2 endpoints
        self._v2_endpoint: str = f"{service_root}/v2/hme"
        self._list_endpoint: str = f"{self._v2_endpoint}/list"
        self._get_endpoint: str = f"{self._v2_endpoint}/get"

    def generate(self) -> str | None:
        """
        Generate a new email alias.

        Returns:
            The generated email address string or None if generation failed.
        """
        req: Response = self.session.post(self._generate_endpoint, params=self.params)
        response: dict[str, dict[str, str]] = req.json()
        result: dict[str, str] | None = response.get("result")
        if result:
            return result.get("hme")
        return None

    def reserve(
        self, email: str, label: str, note: str = "Generated"
    ) -> dict[str, Any]:
        """
        Reserve an alias for emails.

        Args:
            email: The email alias to reserve.
            label: A label for the email alias.
            note: An optional note for the email alias.

        Returns:
            The API's result containing details about the reserved alias.
        """
        req: Response = self.session.post(
            self._reserve_endpoint,
            params=self.params,
            json={
                "hme": email,
                "label": label,
                "note": note,
            },
        )
        response = req.json()
        return cast(dict[str, Any], response.get("result", {}))

    def __len__(self) -> int:
        """
        Get the number of emails
        """
        req: Response = self.session.get(self._list_endpoint, params=self.params)
        response: dict[str, dict[str, str]] = req.json()
        result: dict[str, str] | None = response.get("result")
        if result:
            return len(result.get("hmeEmails", []))
        return 0

    def __iter__(self) -> Generator[Any, Any, None]:
        """
        Iterate over the list of emails
        """
        req: Response = self.session.get(self._list_endpoint, params=self.params)
        response: dict[str, dict[str, str]] = req.json()
        result: dict[str, str] | None = response.get("result")
        if result:
            yield from result.get("hmeEmails", [])

    def __getitem__(self, anonymous_id: str) -> dict[str, Any]:
        """
        Get alias email details by anonymous_id.

        Args:
            anonymous_id: The unique identifier for the alias.

        Returns:
            A dictionary containing details about the alias.
        """
        req: Response = self.session.post(
            self._get_endpoint,
            params=self.params,
            json={"anonymousId": anonymous_id},
        )
        response = req.json()
        return cast(dict[str, Any], response.get("result", {}))

    def update_metadata(
        self, anonymous_id: str, label: str, note: str | None = None
    ) -> dict[str, Any]:
        """
        Update metadata for an alias email.

        Args:
            anonymous_id: The unique identifier for the alias.
            label: The new label for the alias.
            note: The new note for the alias (optional).

        Returns:
            A dictionary containing the API response.
        """
        payload: dict[str, str] = {
            "anonymousId": anonymous_id,
            "label": label,
        }
        if note is not None:
            payload["note"] = note

        req: Response = self.session.post(
            self._update_metadata_endpoint,
            params=self.params,
            json=payload,
        )
        response = req.json()
        return cast(dict[str, Any], response.get("result", {}))

    def delete(self, anonymous_id: str) -> dict[str, Any]:
        """
        Delete an alias email.

        Args:
            anonymous_id: The unique identifier for the alias to delete.

        Returns:
            A dictionary containing the API response.
        """
        req: Response = self.session.post(
            self._delete_endpoint,
            params=self.params,
            json={"anonymousId": anonymous_id},
        )
        response = req.json()
        return cast(dict[str, Any], response.get("result", {}))

    def deactivate(self, anonymous_id: str) -> dict[str, Any]:
        """
        Deactivate an alias email.

        Deactivating an alias means emails sent to it will no longer be forwarded,
        but the alias remains in your list and can be reactivated later.

        Args:
            anonymous_id: The unique identifier for the alias to deactivate.

        Returns:
            A dictionary containing the API response.
        """
        req: Response = self.session.post(
            self._deactivate_endpoint,
            params=self.params,
            json={"anonymousId": anonymous_id},
        )
        response = req.json()
        return cast(dict[str, Any], response.get("result", {}))

    def reactivate(self, anonymous_id: str) -> dict[str, Any]:
        """
        Reactivate a previously deactivated alias email.

        Reactivating an alias means emails sent to it will be forwarded again
        to your primary inbox.

        Args:
            anonymous_id: The unique identifier for the alias to reactivate.

        Returns:
            A dictionary containing the API response.
        """
        req: Response = self.session.post(
            self._reactivate_endpoint,
            params=self.params,
            json={"anonymousId": anonymous_id},
        )
        response = req.json()
        return cast(dict[str, Any], response.get("result", {}))

generate()

Generate a new email alias.

Returns:

Type Description
str | None

The generated email address string or None if generation failed.

Source code in pyicloud/services/hidemyemail.py
46
47
48
49
50
51
52
53
54
55
56
57
58
def generate(self) -> str | None:
    """
    Generate a new email alias.

    Returns:
        The generated email address string or None if generation failed.
    """
    req: Response = self.session.post(self._generate_endpoint, params=self.params)
    response: dict[str, dict[str, str]] = req.json()
    result: dict[str, str] | None = response.get("result")
    if result:
        return result.get("hme")
    return None

reserve(email, label, note='Generated')

Reserve an alias for emails.

Parameters:

Name Type Description Default
email str

The email alias to reserve.

required
label str

A label for the email alias.

required
note str

An optional note for the email alias.

'Generated'

Returns:

Type Description
dict[str, Any]

The API's result containing details about the reserved alias.

Source code in pyicloud/services/hidemyemail.py
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
def reserve(
    self, email: str, label: str, note: str = "Generated"
) -> dict[str, Any]:
    """
    Reserve an alias for emails.

    Args:
        email: The email alias to reserve.
        label: A label for the email alias.
        note: An optional note for the email alias.

    Returns:
        The API's result containing details about the reserved alias.
    """
    req: Response = self.session.post(
        self._reserve_endpoint,
        params=self.params,
        json={
            "hme": email,
            "label": label,
            "note": note,
        },
    )
    response = req.json()
    return cast(dict[str, Any], response.get("result", {}))

update_metadata(anonymous_id, label, note=None)

Update metadata for an alias email.

Parameters:

Name Type Description Default
anonymous_id str

The unique identifier for the alias.

required
label str

The new label for the alias.

required
note str | None

The new note for the alias (optional).

None

Returns:

Type Description
dict[str, Any]

A dictionary containing the API response.

Source code in pyicloud/services/hidemyemail.py
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
def update_metadata(
    self, anonymous_id: str, label: str, note: str | None = None
) -> dict[str, Any]:
    """
    Update metadata for an alias email.

    Args:
        anonymous_id: The unique identifier for the alias.
        label: The new label for the alias.
        note: The new note for the alias (optional).

    Returns:
        A dictionary containing the API response.
    """
    payload: dict[str, str] = {
        "anonymousId": anonymous_id,
        "label": label,
    }
    if note is not None:
        payload["note"] = note

    req: Response = self.session.post(
        self._update_metadata_endpoint,
        params=self.params,
        json=payload,
    )
    response = req.json()
    return cast(dict[str, Any], response.get("result", {}))

delete(anonymous_id)

Delete an alias email.

Parameters:

Name Type Description Default
anonymous_id str

The unique identifier for the alias to delete.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the API response.

Source code in pyicloud/services/hidemyemail.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
def delete(self, anonymous_id: str) -> dict[str, Any]:
    """
    Delete an alias email.

    Args:
        anonymous_id: The unique identifier for the alias to delete.

    Returns:
        A dictionary containing the API response.
    """
    req: Response = self.session.post(
        self._delete_endpoint,
        params=self.params,
        json={"anonymousId": anonymous_id},
    )
    response = req.json()
    return cast(dict[str, Any], response.get("result", {}))

deactivate(anonymous_id)

Deactivate an alias email.

Deactivating an alias means emails sent to it will no longer be forwarded, but the alias remains in your list and can be reactivated later.

Parameters:

Name Type Description Default
anonymous_id str

The unique identifier for the alias to deactivate.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the API response.

Source code in pyicloud/services/hidemyemail.py
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def deactivate(self, anonymous_id: str) -> dict[str, Any]:
    """
    Deactivate an alias email.

    Deactivating an alias means emails sent to it will no longer be forwarded,
    but the alias remains in your list and can be reactivated later.

    Args:
        anonymous_id: The unique identifier for the alias to deactivate.

    Returns:
        A dictionary containing the API response.
    """
    req: Response = self.session.post(
        self._deactivate_endpoint,
        params=self.params,
        json={"anonymousId": anonymous_id},
    )
    response = req.json()
    return cast(dict[str, Any], response.get("result", {}))

reactivate(anonymous_id)

Reactivate a previously deactivated alias email.

Reactivating an alias means emails sent to it will be forwarded again to your primary inbox.

Parameters:

Name Type Description Default
anonymous_id str

The unique identifier for the alias to reactivate.

required

Returns:

Type Description
dict[str, Any]

A dictionary containing the API response.

Source code in pyicloud/services/hidemyemail.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def reactivate(self, anonymous_id: str) -> dict[str, Any]:
    """
    Reactivate a previously deactivated alias email.

    Reactivating an alias means emails sent to it will be forwarded again
    to your primary inbox.

    Args:
        anonymous_id: The unique identifier for the alias to reactivate.

    Returns:
        A dictionary containing the API response.
    """
    req: Response = self.session.post(
        self._reactivate_endpoint,
        params=self.params,
        json={"anonymousId": anonymous_id},
    )
    response = req.json()
    return cast(dict[str, Any], response.get("result", {}))