-
Notifications
You must be signed in to change notification settings - Fork 7
Add missing endpoints #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
IgorDobryn
merged 6 commits into
main
from
MT-21859-python-sdk-add-webhooks-api-tokens-and-sub-accounts-endpoints
May 13, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9da2055
Add .tool-versions with python version
IgorDobryn 3f528e1
Add api_tokens_api#get_api_tokens
IgorDobryn 9f078b3
Add remaining API token endpoints
IgorDobryn afcc75e
Add sub account endpoints
IgorDobryn 2e88bbc
Add webhooks endpoints
IgorDobryn bf0186c
Linter
IgorDobryn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export MAILTRAP_ACCOUNT_ID="op://Mailtrap Dev/Mailtrap SDK Dev API Key/account_id" | ||
| export MAILTRAP_ORGANIZATION_ID="op://Mailtrap Dev/Mailtrap SDK Dev API Key/organization_id" | ||
| export MAILTRAP_API_KEY="op://Mailtrap Dev/Mailtrap SDK Dev API Key/account_api_token" | ||
| export MAILTRAP_ORGANIZATION_API_KEY="op://Mailtrap Dev/Mailtrap SDK Dev API Key/organization_api_token" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| python 3.9.18 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.api_tokens import ApiToken | ||
| from mailtrap.models.api_tokens import ApiTokenWithToken | ||
| from mailtrap.models.common import DeletedObject | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
| ACCOUNT_ID = "YOUR_ACCOUNT_ID" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN) | ||
| api_tokens_api = client.general_api.api_tokens | ||
|
|
||
|
|
||
| def list_api_tokens(account_id: int) -> list[ApiToken]: | ||
| return api_tokens_api.get_list(account_id=account_id) | ||
|
|
||
|
|
||
| def get_api_token(account_id: int, api_token_id: int) -> ApiToken: | ||
| return api_tokens_api.get_by_id(account_id=account_id, api_token_id=api_token_id) | ||
|
|
||
|
|
||
| def create_api_token(account_id: int) -> ApiTokenWithToken: | ||
| # The full token value is only returned once on the response — store it securely. | ||
| return api_tokens_api.create( | ||
| account_id=account_id, | ||
| token_params=mt.CreateApiTokenParams( | ||
| name="My API Token", | ||
| resources=[ | ||
| mt.ApiTokenResource( | ||
| resource_type="account", | ||
| resource_id=account_id, | ||
| access_level=100, | ||
| ) | ||
| ], | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def reset_api_token(account_id: int, api_token_id: int) -> ApiTokenWithToken: | ||
| # The reset response includes the new full token value once — store it securely. | ||
| return api_tokens_api.reset(account_id=account_id, api_token_id=api_token_id) | ||
|
|
||
|
|
||
| def delete_api_token(account_id: int, api_token_id: int) -> DeletedObject: | ||
| return api_tokens_api.delete(account_id=account_id, api_token_id=api_token_id) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tokens = list_api_tokens(ACCOUNT_ID) | ||
| print(tokens) | ||
|
|
||
| created = create_api_token(ACCOUNT_ID) | ||
| print(created) | ||
|
|
||
| fetched = get_api_token(ACCOUNT_ID, created.id) | ||
| print(fetched) | ||
|
|
||
| reset = reset_api_token(ACCOUNT_ID, created.id) | ||
| print(reset) | ||
|
|
||
| deleted = delete_api_token(ACCOUNT_ID, reset.id) | ||
| print(deleted) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.organizations import SubAccount | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
| ORGANIZATION_ID = "YOUR_ORGANIZATION_ID" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN, organization_id=ORGANIZATION_ID) | ||
| sub_accounts_api = client.organizations_api.sub_accounts | ||
|
|
||
|
|
||
| def list_sub_accounts() -> list[SubAccount]: | ||
| return sub_accounts_api.get_list() | ||
|
|
||
|
|
||
| def create_sub_account(name: str) -> SubAccount: | ||
| return sub_accounts_api.create(mt.CreateSubAccountParams(name=name)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sub_accounts = list_sub_accounts() | ||
| print(sub_accounts) | ||
|
|
||
| created = create_sub_account("New Team Account") | ||
| print(created) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import mailtrap as mt | ||
| from mailtrap.models.common import DeletedObject | ||
| from mailtrap.models.webhooks import Webhook | ||
| from mailtrap.models.webhooks import WebhookWithSecret | ||
|
|
||
| API_TOKEN = "YOUR_API_TOKEN" | ||
| ACCOUNT_ID = "YOUR_ACCOUNT_ID" | ||
|
|
||
| client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID) | ||
| webhooks_api = client.webhooks_api.webhooks | ||
|
|
||
|
|
||
| def list_webhooks() -> list[Webhook]: | ||
| return webhooks_api.get_list() | ||
|
|
||
|
|
||
| def get_webhook(webhook_id: int) -> Webhook: | ||
| return webhooks_api.get_by_id(webhook_id=webhook_id) | ||
|
|
||
|
|
||
| def create_webhook() -> WebhookWithSecret: | ||
| # The signing_secret is only returned once on creation — store it | ||
| # securely and use it to verify webhook signatures (HMAC SHA-256). | ||
| return webhooks_api.create( | ||
| mt.CreateWebhookParams( | ||
| url="https://example.com/mailtrap/webhooks", | ||
| webhook_type="email_sending", | ||
| sending_stream="transactional", | ||
| event_types=["delivery", "bounce"], | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def update_webhook(webhook_id: int) -> Webhook: | ||
| return webhooks_api.update( | ||
| webhook_id=webhook_id, | ||
| webhook_params=mt.UpdateWebhookParams(active=False), | ||
| ) | ||
|
|
||
|
|
||
| def delete_webhook(webhook_id: int) -> DeletedObject: | ||
| return webhooks_api.delete(webhook_id=webhook_id) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| webhooks = list_webhooks() | ||
| print(webhooks) | ||
|
|
||
| created = create_webhook() | ||
| print(created) | ||
|
|
||
| fetched = get_webhook(created.id) | ||
| print(fetched) | ||
|
|
||
| updated = update_webhook(created.id) | ||
| print(updated) | ||
|
|
||
| deleted = delete_webhook(created.id) | ||
| print(deleted) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from mailtrap.api.resources.sub_accounts import SubAccountsApi | ||
| from mailtrap.http import HttpClient | ||
|
|
||
|
|
||
| class OrganizationsBaseApi: | ||
| def __init__(self, client: HttpClient, organization_id: str) -> None: | ||
| self._organization_id = organization_id | ||
| self._client = client | ||
|
|
||
| @property | ||
| def sub_accounts(self) -> SubAccountsApi: | ||
| return SubAccountsApi(organization_id=self._organization_id, client=self._client) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from typing import Optional | ||
|
|
||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.api_tokens import ApiToken | ||
| from mailtrap.models.api_tokens import ApiTokenWithToken | ||
| from mailtrap.models.api_tokens import CreateApiTokenParams | ||
| from mailtrap.models.common import DeletedObject | ||
|
|
||
|
|
||
| class ApiTokensApi: | ||
| def __init__(self, client: HttpClient) -> None: | ||
| self._client = client | ||
|
|
||
| def get_list(self, account_id: int) -> list[ApiToken]: | ||
| """ | ||
| Returns all API tokens visible to the current API token. | ||
| """ | ||
| response = self._client.get(self._api_path(account_id)) | ||
| return [ApiToken(**api_token) for api_token in response] | ||
|
|
||
| def get_by_id(self, account_id: int, api_token_id: int) -> ApiToken: | ||
| """ | ||
| Get a single API token by id. | ||
| """ | ||
| response = self._client.get(self._api_path(account_id, api_token_id)) | ||
| return ApiToken(**response) | ||
|
|
||
| def create( | ||
| self, account_id: int, token_params: CreateApiTokenParams | ||
| ) -> ApiTokenWithToken: | ||
| """ | ||
| Create a new API token. The full token value is only returned once | ||
| in the response — store it securely. | ||
| """ | ||
| response = self._client.post( | ||
| self._api_path(account_id), json=token_params.api_data | ||
| ) | ||
| return ApiTokenWithToken(**response) | ||
|
|
||
| def delete(self, account_id: int, api_token_id: int) -> DeletedObject: | ||
| """ | ||
| Permanently delete an API token. | ||
| """ | ||
| self._client.delete(self._api_path(account_id, api_token_id)) | ||
| return DeletedObject(id=api_token_id) | ||
|
|
||
| def reset(self, account_id: int, api_token_id: int) -> ApiTokenWithToken: | ||
| """ | ||
| Expire the requested token and create a new token with the same | ||
| permissions. The full new token value is returned once — store it | ||
| securely. Only tokens that have not already been reset can be reset. | ||
| """ | ||
| response = self._client.post(f"{self._api_path(account_id, api_token_id)}/reset") | ||
| return ApiTokenWithToken(**response) | ||
|
|
||
| @staticmethod | ||
| def _api_path(account_id: int, api_token_id: Optional[int] = None) -> str: | ||
| path = f"/api/accounts/{account_id}/api_tokens" | ||
| if api_token_id is not None: | ||
| return f"{path}/{api_token_id}" | ||
| return path |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.organizations import CreateSubAccountParams | ||
| from mailtrap.models.organizations import SubAccount | ||
|
|
||
|
|
||
| class SubAccountsApi: | ||
| def __init__(self, client: HttpClient, organization_id: str) -> None: | ||
| self._organization_id = organization_id | ||
| self._client = client | ||
|
|
||
| def get_list(self) -> list[SubAccount]: | ||
| """ | ||
| Get a list of sub accounts for the organization. Requires sub | ||
| account management permissions for this organization. | ||
| """ | ||
| response = self._client.get(self._api_path()) | ||
| return [SubAccount(**sub_account) for sub_account in response] | ||
|
|
||
| def create(self, sub_account_params: CreateSubAccountParams) -> SubAccount: | ||
| """ | ||
| Create a new sub account under the organization. Requires sub | ||
| account management permissions for this organization. | ||
| """ | ||
| response = self._client.post( | ||
| self._api_path(), | ||
| json={"account": sub_account_params.api_data}, | ||
| ) | ||
| return SubAccount(**response) | ||
|
|
||
| def _api_path(self) -> str: | ||
| return f"/api/organizations/{self._organization_id}/sub_accounts" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from typing import Optional | ||
|
|
||
| from mailtrap.http import HttpClient | ||
| from mailtrap.models.common import DeletedObject | ||
| from mailtrap.models.webhooks import CreateWebhookParams | ||
| from mailtrap.models.webhooks import UpdateWebhookParams | ||
| from mailtrap.models.webhooks import Webhook | ||
| from mailtrap.models.webhooks import WebhookCreateResponse | ||
| from mailtrap.models.webhooks import WebhookListResponse | ||
| from mailtrap.models.webhooks import WebhookResponse | ||
| from mailtrap.models.webhooks import WebhookWithSecret | ||
|
|
||
|
|
||
| class WebhooksApi: | ||
| def __init__(self, client: HttpClient, account_id: str) -> None: | ||
| self._account_id = account_id | ||
| self._client = client | ||
|
|
||
| def get_list(self) -> list[Webhook]: | ||
| """ | ||
| List all webhooks for the account. | ||
| """ | ||
| response = self._client.get(self._api_path()) | ||
| return WebhookListResponse(**response).data | ||
|
|
||
| def get_by_id(self, webhook_id: int) -> Webhook: | ||
| """ | ||
| Get a single webhook by id. | ||
| """ | ||
| response = self._client.get(self._api_path(webhook_id)) | ||
| return WebhookResponse(**response).data | ||
|
|
||
| def create(self, webhook_params: CreateWebhookParams) -> WebhookWithSecret: | ||
| """ | ||
| Create a new webhook. The response includes a `signing_secret` used | ||
| to verify webhook signatures — store it securely; it is only | ||
| returned once on creation. | ||
| """ | ||
| response = self._client.post( | ||
| self._api_path(), json={"webhook": webhook_params.api_data} | ||
| ) | ||
| return WebhookCreateResponse(**response).data | ||
|
|
||
| def update(self, webhook_id: int, webhook_params: UpdateWebhookParams) -> Webhook: | ||
| """ | ||
| Update an existing webhook. Only the fields supplied in | ||
| `webhook_params` are sent to the API. | ||
| """ | ||
| response = self._client.patch( | ||
| self._api_path(webhook_id), | ||
| json={"webhook": webhook_params.api_data}, | ||
| ) | ||
| return WebhookResponse(**response).data | ||
|
|
||
| def delete(self, webhook_id: int) -> DeletedObject: | ||
| """ | ||
| Permanently delete a webhook. | ||
| """ | ||
| self._client.delete(self._api_path(webhook_id)) | ||
| return DeletedObject(id=webhook_id) | ||
|
|
||
| def _api_path(self, webhook_id: Optional[int] = None) -> str: | ||
| path = f"/api/accounts/{self._account_id}/webhooks" | ||
| if webhook_id is not None: | ||
| return f"{path}/{webhook_id}" | ||
| return path |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.9 is quite old already. Is the idea to use the the minimum version we support for development?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes