Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## v7.4.0 (2026-02-02)

- Adds the following functions usable by child and referral customer users:
- `api_key.create`
- `api_key.delete`
- `api_key.enable`
- `api_key.disable`

## v7.3.0 (2025-11-24)

- Adds the following functions:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.3.0
7.4.0
33 changes: 32 additions & 1 deletion lib/easypost/services/api_key.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

class EasyPost::Services::ApiKey < EasyPost::Services::Service
MODEL_CLASS = EasyPost::Models::ApiKey # :nodoc:

# Retrieve a list of all ApiKey objects.
def all
response = @client.make_request(:get, 'api_keys')

EasyPost::InternalUtilities::Json.convert_json_to_object(response, EasyPost::Models::ApiKey)
EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end

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

raise EasyPost::Errors::FilteringError.new(EasyPost::Constants::NO_USER_FOUND)
end

# Create an API key for a child or referral customer user
def create(mode)
response = @client.make_request(:post, 'api_keys', { mode: mode })

EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end

# Delete an API key for a child or referral customer user
def delete(id)
@client.make_request(:delete, "api_keys/#{id}")

# Return true if succeeds, an error will be thrown if it fails
true
end

# Enable an API key for a child or referral customer user
def enable(id)
response = @client.make_request(:post, "api_keys/#{id}/enable")

EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end

# Disable an API key for a child or referral customer user
def disable(id)
response = @client.make_request(:post, "api_keys/#{id}/disable")

EasyPost::InternalUtilities::Json.convert_json_to_object(response, MODEL_CLASS)
end
end
23 changes: 23 additions & 0 deletions spec/api_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,27 @@
client.user.delete(user.id) # delete the user so we don't clutter the test environment
end
end

describe 'API key lifecycle' do
let(:referral_client) { EasyPost::Client.new(api_key: ENV['REFERRAL_CUSTOMER_PROD_API_KEY'] || '123') }

it 'creates, enables, disables, and deletes an API key for a referral customer' do
# Create
api_key = referral_client.api_key.create('production')
expect(api_key).to be_an_instance_of(EasyPost::Models::ApiKey)
expect(api_key.id).to match('ak_')
expect(api_key.mode).to eq('production')

# Disable
disabled_key = referral_client.api_key.disable(api_key.id)
expect(disabled_key.active).to be false

# Enable
enabled_key = referral_client.api_key.enable(api_key.id)
expect(enabled_key.active).to be true

# Delete
referral_client.api_key.delete(api_key.id)
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.