Skip to content

Commit 462a309

Browse files
authored
feat: create/delete/enable/disable API keys (#642)
# Description <!-- Please provide a general summary of your PR changes and link any related issues or other pull requests. --> # Testing <!-- Please provide details on how you tested this code. See below. - All pull requests must be tested (unit tests where possible with accompanying cassettes, or provide a screenshot of end-to-end testing when unit tests are not possible) - New features must get a new unit test - Bug fixes/refactors must re-record existing cassettes --> # Pull Request Type Please select the option(s) that are relevant to this PR. - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Improvement (fixing a typo, updating readme, renaming a variable name, etc)
1 parent 575a5e7 commit 462a309

7 files changed

Lines changed: 306 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# CHANGELOG
22

3+
## v7.5.0 (2026-02-03)
4+
5+
- Adds the following functions usable by child and referral customer users:
6+
- `ApiKey.Create`
7+
- `ApiKey.Delete`
8+
- `ApiKey.Enable`
9+
- `ApiKey.Disable`
10+
311
## v7.4.0 (2025-11-24)
412

513
- Adds .NET 10 support

EasyPost.Tests/ServicesTests/ApiKeyServiceTest.cs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,33 @@ public ApiKeyServiceTests() : base("api_key_service", TestUtils.ApiKey.Productio
2020

2121
#region Test CRUD Operations
2222

23+
[Fact]
24+
[CrudOperations.Read]
25+
public async Task TestRetrieveApiKeys()
26+
{
27+
UseVCR("retrieve_api_keys");
28+
29+
User user = await Client.User.RetrieveMe();
30+
31+
List<ApiKey> apiKeys = await Client.ApiKey.RetrieveApiKeysForUser(user.Id);
32+
33+
Assert.IsType<List<ApiKey>>(apiKeys);
34+
}
35+
36+
[Fact]
37+
[CrudOperations.Read]
38+
public async Task TestRetrieveApiKeysChild()
39+
{
40+
UseVCR("retrieve_api_keys_child");
41+
42+
const string fakeChildId = "user_123456789";
43+
44+
// Test suite user has no child users, so this should throw a FilteringError
45+
Exception? possibleException = await Record.ExceptionAsync(async () => await Client.ApiKey.RetrieveApiKeysForUser(fakeChildId));
46+
47+
Assert.IsType<FilteringError>(possibleException);
48+
}
49+
2350
[Fact]
2451
[CrudOperations.Read]
2552
[Testing.Function]
@@ -43,30 +70,30 @@ public async Task TestAll()
4370
}
4471

4572
[Fact]
46-
[CrudOperations.Read]
47-
public async Task TestRetrieveApiKeys()
73+
[CrudOperations.Create]
74+
[CrudOperations.Update]
75+
[CrudOperations.Delete]
76+
public async Task TestApiKeyLifecycle()
4877
{
49-
UseVCR("retrieve_api_keys");
78+
string? referralApiKey = Environment.GetEnvironmentVariable("REFERRAL_CUSTOMER_PROD_API_KEY");
79+
UseVCR("lifecycle", referralApiKey);
5080

51-
User user = await Client.User.RetrieveMe();
81+
// Create an API key
82+
ApiKey apiKey = await Client.ApiKey.Create("production");
83+
Assert.NotNull(apiKey);
84+
Assert.StartsWith("ak_", apiKey.Id);
85+
Assert.Equal("production", apiKey.Mode);
5286

53-
List<ApiKey> apiKeys = await Client.ApiKey.RetrieveApiKeysForUser(user.Id);
87+
// Disable the API key
88+
apiKey = await Client.ApiKey.Disable(apiKey.Id);
89+
Assert.False(apiKey.Active);
5490

55-
Assert.IsType<List<ApiKey>>(apiKeys);
56-
}
91+
// Enable the API key
92+
apiKey = await Client.ApiKey.Enable(apiKey.Id);
93+
Assert.True(apiKey.Active);
5794

58-
[Fact]
59-
[CrudOperations.Read]
60-
public async Task TestRetrieveApiKeysChild()
61-
{
62-
UseVCR("retrieve_api_keys_child");
63-
64-
const string fakeChildId = "user_123456789";
65-
66-
// Test suite user has no child users, so this should throw a FilteringError
67-
Exception? possibleException = await Record.ExceptionAsync(async () => await Client.ApiKey.RetrieveApiKeysForUser(fakeChildId));
68-
69-
Assert.IsType<FilteringError>(possibleException);
95+
// Delete the API key
96+
await Client.ApiKey.Delete(apiKey.Id);
7097
}
7198

7299
#endregion

EasyPost.Tests/cassettes/net/api_key_service/lifecycle.json

Lines changed: 184 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

EasyPost.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<metadata>
44
<id>EasyPost-Official</id>
55
<title>EasyPost (Official)</title>
6-
<version>7.4.0</version>
6+
<version>7.5.0</version>
77
<authors>EasyPost</authors>
88
<owners>EasyPost</owners>
99
<projectUrl>https://www.easypost.com</projectUrl>

EasyPost/Models/API/ApiKey.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@ public class ApiKey : EasyPostObject
1111
{
1212
#region JSON Properties
1313

14+
/// <summary>
15+
/// Whether the API key is active.
16+
/// </summary>
17+
[JsonProperty("active")]
18+
public bool? Active { get; set; }
19+
1420
/// <summary>
1521
/// The actual key value to use for authentication.
1622
/// </summary>
1723
[JsonProperty("key")]
1824
public string? Key { get; set; }
1925

26+
/// <summary>
27+
/// The mode of the API key (e.g., "production" or "test").
28+
/// </summary>
29+
[JsonProperty("mode")]
30+
public new string? Mode { get; set; }
31+
2032
#endregion
2133
}
2234

EasyPost/Properties/VersionInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
// Version information for an assembly must follow semantic versioning
44
// When releasing a release candidate, append a 4th digit being the number of the release candidate
5-
[assembly: AssemblyVersion("7.4.0")]
6-
[assembly: AssemblyFileVersion("7.4.0")]
7-
[assembly: AssemblyInformationalVersion("7.4.0")]
5+
[assembly: AssemblyVersion("7.5.0")]
6+
[assembly: AssemblyFileVersion("7.5.0")]
7+
[assembly: AssemblyInformationalVersion("7.5.0")]

0 commit comments

Comments
 (0)