Skip to content

Commit c260b00

Browse files
authored
feat: create/delete/enable/disable API keys (#337)
# 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 71c2a0f commit c260b00

File tree

5 files changed

+317
-2
lines changed

5 files changed

+317
-2
lines changed

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.4.0 (2026-02-02)
4+
5+
- Adds the following functions usable by child and referral customer users:
6+
- `api_key.create`
7+
- `api_key.delete`
8+
- `api_key.enable`
9+
- `api_key.disable`
10+
311
## v7.3.0 (2025-11-24)
412

513
- Adds the following functions:

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.3.0
1+
7.4.0

lib/easypost/services/api_key.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# frozen_string_literal: true
22

33
class EasyPost::Services::ApiKey < EasyPost::Services::Service
4+
MODEL_CLASS = EasyPost::Models::ApiKey # :nodoc:
5+
46
# Retrieve a list of all ApiKey objects.
57
def all
68
response = @client.make_request(:get, 'api_keys')
79

8-
EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::ApiKey)
10+
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
911
end
1012

1113
# Retrieve a list of ApiKey objects (works for the authenticated user or a child user).
@@ -26,4 +28,33 @@ def retrieve_api_keys_for_user(id)
2628

2729
raise EasyPost::Errors::FilteringError.new(EasyPost::Constants::NO_USER_FOUND)
2830
end
31+
32+
# Create an API key for a child or referral customer user
33+
def create(mode)
34+
response = @client.make_request(:post, 'api_keys', { mode: mode })
35+
36+
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
37+
end
38+
39+
# Delete an API key for a child or referral customer user
40+
def delete(id)
41+
@client.make_request(:delete, "api_keys/#{id}")
42+
43+
# Return true if succeeds, an error will be thrown if it fails
44+
true
45+
end
46+
47+
# Enable an API key for a child or referral customer user
48+
def enable(id)
49+
response = @client.make_request(:post, "api_keys/#{id}/enable")
50+
51+
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
52+
end
53+
54+
# Disable an API key for a child or referral customer user
55+
def disable(id)
56+
response = @client.make_request(:post, "api_keys/#{id}/disable")
57+
58+
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
59+
end
2960
end

spec/api_key_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,27 @@
3737
client.user.delete(user.id) # delete the user so we don't clutter the test environment
3838
end
3939
end
40+
41+
describe 'API key lifecycle' do
42+
let(:referral_client) { EasyPost::Client.new(api_key: ENV['REFERRAL_CUSTOMER_PROD_API_KEY'] || '123') }
43+
44+
it 'creates, enables, disables, and deletes an API key for a referral customer' do
45+
# Create
46+
api_key = referral_client.api_key.create('production')
47+
expect(api_key).to be_an_instance_of(EasyPost::Models::ApiKey)
48+
expect(api_key.id).to match('ak_')
49+
expect(api_key.mode).to eq('production')
50+
51+
# Disable
52+
disabled_key = referral_client.api_key.disable(api_key.id)
53+
expect(disabled_key.active).to be false
54+
55+
# Enable
56+
enabled_key = referral_client.api_key.enable(api_key.id)
57+
expect(enabled_key.active).to be true
58+
59+
# Delete
60+
referral_client.api_key.delete(api_key.id)
61+
end
62+
end
4063
end

spec/cassettes/api_key/EasyPost_Services_ApiKey_API_key_lifecycle_creates_enables_disables_and_deletes_an_API_key_for_a_referral_customer.yml

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

0 commit comments

Comments
 (0)