Skip to content

Commit 4d01076

Browse files
authored
Merge pull request #240 from mailtrap/MT-21867-add-missing-endpoints
Add missing endpoints
2 parents 78b8b4d + 0725079 commit 4d01076

88 files changed

Lines changed: 2644 additions & 5 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.envrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export MAILTRAP_ACCOUNT_ID="op://Mailtrap Dev/Mailtrap SDK Dev API Key/account_id"
2+
export MAILTRAP_ORGANIZATION_ID="op://Mailtrap Dev/Mailtrap SDK Dev API Key/organization_id"
3+
export MAILTRAP_API_KEY="op://Mailtrap Dev/Mailtrap SDK Dev API Key/account_api_token"
4+
export MAILTRAP_ORGANIZATION_API_KEY="op://Mailtrap Dev/Mailtrap SDK Dev API Key/organization_api_token"

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## [Unreleased]
4+
5+
### Features
6+
7+
- **API Tokens API** — Added full CRUD support:
8+
- `IAccountResource.ApiTokens().GetAll()``GET /api/accounts/{account_id}/api_tokens`
9+
- `IAccountResource.ApiTokens().Create(request)``POST /api/accounts/{account_id}/api_tokens`
10+
- `IAccountResource.ApiToken(id).GetDetails()``GET /api/accounts/{account_id}/api_tokens/{id}`
11+
- `IAccountResource.ApiToken(id).Delete()``DELETE /api/accounts/{account_id}/api_tokens/{id}`
12+
- `IAccountResource.ApiToken(id).Reset()``POST /api/accounts/{account_id}/api_tokens/{id}/reset`
13+
- **Organizations API** — Added new top-level `IMailtrapOrganizationClient` (spawned via `MailtrapClientFactory.CreateOrganizationClient()`) for organization-scoped operations:
14+
- `IOrganizationResource.SubAccounts().GetAll()``GET /api/organizations/{organization_id}/sub_accounts`
15+
- `IOrganizationResource.SubAccounts().Create(request)``POST /api/organizations/{organization_id}/sub_accounts`
16+
317
## [3.1.1] - 2026-03-30
418

519
### Fixes & Maintenance

Mailtrap.sln

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mailtrap.Example.Stats", "e
110110
EndProject
111111
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mailtrap.Example.EmailLogs", "examples\Mailtrap.Example.EmailLogs\Mailtrap.Example.EmailLogs.csproj", "{C4F8B2D3-5E6A-7F9B-0C1D-2E3F4A5B6C7D}"
112112
EndProject
113+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mailtrap.Example.ApiTokens", "examples\Mailtrap.Example.ApiTokens\Mailtrap.Example.ApiTokens.csproj", "{D5E6F7A8-1234-5678-9ABC-DEF012345601}"
114+
EndProject
115+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mailtrap.Example.Webhooks", "examples\Mailtrap.Example.Webhooks\Mailtrap.Example.Webhooks.csproj", "{D5E6F7A8-1234-5678-9ABC-DEF012345602}"
116+
EndProject
117+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mailtrap.Example.SubAccount", "examples\Mailtrap.Example.SubAccount\Mailtrap.Example.SubAccount.csproj", "{D5E6F7A8-1234-5678-9ABC-DEF012345603}"
118+
EndProject
113119
Global
114120
GlobalSection(SolutionConfigurationPlatforms) = preSolution
115121
Debug|Any CPU = Debug|Any CPU
@@ -228,6 +234,18 @@ Global
228234
{C4F8B2D3-5E6A-7F9B-0C1D-2E3F4A5B6C7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
229235
{C4F8B2D3-5E6A-7F9B-0C1D-2E3F4A5B6C7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
230236
{C4F8B2D3-5E6A-7F9B-0C1D-2E3F4A5B6C7D}.Release|Any CPU.Build.0 = Release|Any CPU
237+
{D5E6F7A8-1234-5678-9ABC-DEF012345601}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
238+
{D5E6F7A8-1234-5678-9ABC-DEF012345601}.Debug|Any CPU.Build.0 = Debug|Any CPU
239+
{D5E6F7A8-1234-5678-9ABC-DEF012345601}.Release|Any CPU.ActiveCfg = Release|Any CPU
240+
{D5E6F7A8-1234-5678-9ABC-DEF012345601}.Release|Any CPU.Build.0 = Release|Any CPU
241+
{D5E6F7A8-1234-5678-9ABC-DEF012345602}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
242+
{D5E6F7A8-1234-5678-9ABC-DEF012345602}.Debug|Any CPU.Build.0 = Debug|Any CPU
243+
{D5E6F7A8-1234-5678-9ABC-DEF012345602}.Release|Any CPU.ActiveCfg = Release|Any CPU
244+
{D5E6F7A8-1234-5678-9ABC-DEF012345602}.Release|Any CPU.Build.0 = Release|Any CPU
245+
{D5E6F7A8-1234-5678-9ABC-DEF012345603}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
246+
{D5E6F7A8-1234-5678-9ABC-DEF012345603}.Debug|Any CPU.Build.0 = Debug|Any CPU
247+
{D5E6F7A8-1234-5678-9ABC-DEF012345603}.Release|Any CPU.ActiveCfg = Release|Any CPU
248+
{D5E6F7A8-1234-5678-9ABC-DEF012345603}.Release|Any CPU.Build.0 = Release|Any CPU
231249
EndGlobalSection
232250
GlobalSection(SolutionProperties) = preSolution
233251
HideSolutionNode = FALSE
@@ -261,6 +279,9 @@ Global
261279
{11894B20-F780-4FC9-8140-3601EBF54C10} = {09E18837-1DDE-4EAF-80EC-DA55557C81EB}
262280
{B3E7A1C2-4D5F-6E8A-9B0C-1D2E3F4A5B6C} = {09E18837-1DDE-4EAF-80EC-DA55557C81EB}
263281
{C4F8B2D3-5E6A-7F9B-0C1D-2E3F4A5B6C7D} = {09E18837-1DDE-4EAF-80EC-DA55557C81EB}
282+
{D5E6F7A8-1234-5678-9ABC-DEF012345601} = {09E18837-1DDE-4EAF-80EC-DA55557C81EB}
283+
{D5E6F7A8-1234-5678-9ABC-DEF012345602} = {09E18837-1DDE-4EAF-80EC-DA55557C81EB}
284+
{D5E6F7A8-1234-5678-9ABC-DEF012345603} = {09E18837-1DDE-4EAF-80EC-DA55557C81EB}
264285
EndGlobalSection
265286
GlobalSection(ExtensibilityGlobals) = postSolution
266287
SolutionGuid = {0FF614CC-FEBC-4C66-B3FC-FCB73EE511D7}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ private static SendEmailRequest TemplateBasedRequest()
276276
- Suppressions management – [`examples/Mailtrap.Example.Suppression`](examples/Mailtrap.Example.Suppression/)
277277
- Email logs (list with filters and pagination; get message details with events) – [`examples/Mailtrap.Example.EmailLogs`](examples/Mailtrap.Example.EmailLogs/)
278278
- Email sending statistics – [`examples/Mailtrap.Example.Stats`](examples/Mailtrap.Example.Stats/)
279+
- Webhooks management – [`examples/Mailtrap.Example.Webhooks`](examples/Mailtrap.Example.Webhooks/)
279280

280281
### Email Sandbox (Testing)
281282

@@ -301,6 +302,8 @@ private static SendEmailRequest TemplateBasedRequest()
301302
- Account access management – [`examples/Mailtrap.Example.AccountAccess`](examples/Mailtrap.Example.AccountAccess/)
302303
- Permissions management – [`examples/Mailtrap.Example.Permissions`](examples/Mailtrap.Example.Permissions/)
303304
- Accounts management – [`examples/Mailtrap.Example.Account`](examples/Mailtrap.Example.Account/)
305+
- Sub accounts management – [`examples/Mailtrap.Example.SubAccount`](examples/Mailtrap.Example.SubAccount/)
306+
- API tokens management – [`examples/Mailtrap.Example.ApiTokens`](examples/Mailtrap.Example.ApiTokens/)
304307
- Billing information – [`examples/Mailtrap.Example.Billing`](examples/Mailtrap.Example.Billing/)
305308
- Comprehensive API Usage – [`examples/Mailtrap.Example.ApiUsage`](examples/Mailtrap.Example.ApiUsage/)
306309

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<Project Sdk="Microsoft.NET.Sdk" />
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using Mailtrap;
2+
using Mailtrap.Accounts;
3+
using Mailtrap.ApiTokens;
4+
using Mailtrap.ApiTokens.Models;
5+
using Mailtrap.ApiTokens.Requests;
6+
using Mailtrap.ApiTokens.Responses;
7+
using Mailtrap.Core.Models;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.DependencyInjection;
10+
using Microsoft.Extensions.Hosting;
11+
using Microsoft.Extensions.Logging;
12+
13+
14+
HostApplicationBuilder hostBuilder = Host.CreateApplicationBuilder(args);
15+
16+
IConfigurationSection config = hostBuilder.Configuration.GetSection("Mailtrap");
17+
18+
hostBuilder.Services.AddMailtrapClient(config);
19+
20+
using IHost host = hostBuilder.Build();
21+
22+
ILogger<Program> logger = host.Services.GetRequiredService<ILogger<Program>>();
23+
IMailtrapClient mailtrapClient = host.Services.GetRequiredService<IMailtrapClient>();
24+
25+
try
26+
{
27+
var accountId = 12345;
28+
var inboxId = 67890;
29+
IAccountResource accountResource = mailtrapClient.Account(accountId);
30+
31+
// Get resource for API tokens collection
32+
IApiTokenCollectionResource apiTokensResource = accountResource.ApiTokens();
33+
34+
// List all API tokens visible to the current API token
35+
IList<ApiToken> apiTokens = await apiTokensResource.GetAll();
36+
logger.LogInformation("Found {Count} API token(s).", apiTokens.Count);
37+
38+
// Create a new API token scoped to a specific inbox with Viewer access
39+
var createRequest = new CreateApiTokenRequest
40+
{
41+
Name = "Demo Viewer Token"
42+
};
43+
createRequest.Resources.Add(new ApiTokenAccessRequest(ResourceType.Inbox, inboxId, AccessLevel.Viewer));
44+
45+
CreateApiTokenResponse createdToken = await apiTokensResource.Create(createRequest);
46+
47+
// The full token value is only returned at creation time - store it securely
48+
logger.LogInformation(
49+
"Created API Token: Id={Id}, Name={Name}, Last4={Last4}",
50+
createdToken.Id,
51+
createdToken.Name,
52+
createdToken.Last4Digits);
53+
logger.LogInformation("Full token value (store securely, returned only once): {Token}", createdToken.Token);
54+
55+
// Get resource for the specific API token
56+
IApiTokenResource apiTokenResource = accountResource.ApiToken(createdToken.Id);
57+
58+
// Get details of the API token
59+
ApiToken tokenDetails = await apiTokenResource.GetDetails();
60+
logger.LogInformation("Token details: Id={Id}, Name={Name}, CreatedBy={CreatedBy}",
61+
tokenDetails.Id,
62+
tokenDetails.Name,
63+
tokenDetails.CreatedBy);
64+
65+
// Reset the API token - expires the current token and returns a new one with the same permissions
66+
ApiTokenResetResponse resetResponse = await apiTokenResource.Reset();
67+
logger.LogInformation(
68+
"Reset API Token: Id={Id}, NewLast4={Last4}",
69+
resetResponse.Id,
70+
resetResponse.Last4Digits);
71+
logger.LogInformation("New token value (store securely, returned only once): {Token}", resetResponse.Token);
72+
73+
// Delete the API token
74+
// The API token resource becomes invalid after deletion and should not be used anymore
75+
await apiTokenResource.Delete();
76+
logger.LogInformation("API Token Deleted.");
77+
}
78+
catch (Exception ex)
79+
{
80+
logger.LogError(ex, "An error occurred during API call.");
81+
Environment.ExitCode = 1;
82+
return;
83+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"profiles": {
3+
"Project": {
4+
"commandName": "Project",
5+
"environmentVariables": {
6+
"DOTNET_ENVIRONMENT": "Development"
7+
}
8+
}
9+
}
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"System": "Warning",
6+
"Microsoft": "Warning"
7+
},
8+
"Debug": {
9+
"LogLevel": {
10+
"Default": "Debug"
11+
}
12+
}
13+
},
14+
"Mailtrap": {
15+
"ApiToken": "<API_KEY>"
16+
}
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<Project Sdk="Microsoft.NET.Sdk" />
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Mailtrap;
2+
using Mailtrap.Organizations;
3+
using Mailtrap.Organizations.Models;
4+
using Mailtrap.Organizations.Requests;
5+
using Microsoft.Extensions.Configuration;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
11+
HostApplicationBuilder hostBuilder = Host.CreateApplicationBuilder(args);
12+
13+
IConfigurationSection config = hostBuilder.Configuration.GetSection("Mailtrap");
14+
15+
hostBuilder.Services.AddMailtrapClient(config);
16+
17+
using IHost host = hostBuilder.Build();
18+
19+
ILogger<Program> logger = host.Services.GetRequiredService<ILogger<Program>>();
20+
21+
// Sub accounts live under /api/organizations/{organization_id}/... so they are
22+
// accessed via the organization-scoped client, not IMailtrapClient.
23+
IMailtrapOrganizationClient organizationClient = host.Services.GetRequiredService<IMailtrapOrganizationClient>();
24+
25+
try
26+
{
27+
var organizationId = 12345;
28+
IOrganizationResource organizationResource = organizationClient.Organization(organizationId);
29+
30+
// Get resource for sub accounts collection
31+
IOrganizationSubAccountCollectionResource subAccountsResource = organizationResource.SubAccounts();
32+
33+
// List sub accounts of the organization
34+
IList<SubAccount> subAccounts = await subAccountsResource.GetAll();
35+
logger.LogInformation("Found {Count} sub account(s).", subAccounts.Count);
36+
37+
foreach (SubAccount subAccount in subAccounts)
38+
{
39+
logger.LogInformation("Sub Account: Id={Id}, Name={Name}", subAccount.Id, subAccount.Name);
40+
}
41+
42+
// Create a new sub account under the organization
43+
var createRequest = new CreateSubAccountRequest
44+
{
45+
Account = new SubAccountAttributes
46+
{
47+
Name = "Demo Sub Account"
48+
}
49+
};
50+
SubAccount createdSubAccount = await subAccountsResource.Create(createRequest);
51+
logger.LogInformation(
52+
"Created Sub Account: Id={Id}, Name={Name}",
53+
createdSubAccount.Id,
54+
createdSubAccount.Name);
55+
}
56+
catch (Exception ex)
57+
{
58+
logger.LogError(ex, "An error occurred during API call.");
59+
Environment.ExitCode = 1;
60+
return;
61+
}

0 commit comments

Comments
 (0)