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

## v8.5.0 (2026-02-03)

- Adds the following functions usable by child and referral customer users (closes #375):
- `apiKey.create`
- `apiKey.delete`
- `apiKey.enable`
- `apiKey.disable`

## v8.4.0 (2025-11-24)

- Adds the following functions:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "easypost/easypost-php",
"description": "EasyPost Shipping API Client Library for PHP",
"version": "8.4.0",
"version": "8.5.0",
"keywords": [
"shipping",
"api",
Expand Down
2 changes: 1 addition & 1 deletion lib/EasyPost/Constant/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class Constants
const BETA_API_VERSION = 'beta';

// Library constants
const LIBRARY_VERSION = '8.4.0';
const LIBRARY_VERSION = '8.5.0';
const SUPPORT_EMAIL = 'support@easypost.com';

// Validation
Expand Down
75 changes: 62 additions & 13 deletions lib/EasyPost/Service/ApiKeyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@
*/
class ApiKeyService extends BaseService
{
/**
* Retrieve a list of all API keys.
*
* @return mixed
*/
public function all(): mixed
{
$response = Requestor::request($this->client, 'get', '/api_keys');

return InternalUtil::convertToEasyPostObject($this->client, $response);
}

/**
* Retrieve a list of API keys (works for the authenticated user or a child user).
*
Expand All @@ -40,7 +28,6 @@ public function retrieveApiKeysForUser(string $id): mixed
return $apiKeys->keys;
}


// This function was called on a child user, authenticated as parent, only return this child user's details
foreach ($apiKeys->children as $childrenKeys) {
if ($childrenKeys->id == $id) {
Expand All @@ -50,4 +37,66 @@ public function retrieveApiKeysForUser(string $id): mixed

throw new FilteringException(Constants::NO_USER_FOUND_ERROR);
}

/**
* Retrieve a list of all API keys.
*
* @return mixed
*/
public function all(): mixed
{
$response = Requestor::request($this->client, 'get', '/api_keys');

return InternalUtil::convertToEasyPostObject($this->client, $response);
}

/**
* Create an API key for a child or referral customer user.
*
* @param string $mode
* @return mixed
*/
public function create(string $mode): mixed
{
$params = ['mode' => $mode];

return self::createResource(self::serviceModelClassName(self::class), $params);
}

/**
* Delete an API key for a child or referral customer user.
*
* @param string $id
* @return void
*/
public function delete(string $id): void
{
$this->deleteResource(self::serviceModelClassName(self::class), $id);
}

/**
* Enable a child or referral customer API key.
*
* @param string $id
* @return mixed
*/
public function enable(string $id): mixed
{
$response = Requestor::request($this->client, 'post', "/api_keys/{$id}/enable");

return InternalUtil::convertToEasyPostObject($this->client, $response);
}

/**
* Disable a child or referral customer API key.
*
* @param string $id
* @return mixed
*/
public function disable(string $id): mixed
{
$response = Requestor::request($this->client, 'post', "/api_keys/{$id}/disable");

return InternalUtil::convertToEasyPostObject($this->client, $response);
}
}
60 changes: 43 additions & 17 deletions test/EasyPost/ApiKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,6 @@ public static function tearDownAfterClass(): void
TestUtil::teardownVcrTests();
}

/**
* Test retrieving all API keys.
*/
public function testAllApiKeys(): void
{
TestUtil::setupCassette('apiKeys/allApiKeys.yml');

$apiKeys = self::$client->apiKeys->all();

$this->assertContainsOnlyInstancesOf(ApiKey::class, $apiKeys['keys']);
foreach ($apiKeys['children'] as $child) {
$this->assertContainsOnlyInstancesOf(ApiKey::class, $child['keys']);
}
}

/**
* Test retrieving the authenticated user's API keys.
*/
Expand All @@ -59,9 +44,9 @@ public function testAuthenticatedUserApiKeys(): void
/**
* Test retrieving the authenticated user's API keys.
*/
public function testChildUserApiKeys(): void
public function testRetrieveChildUserApiKeys(): void
{
TestUtil::setupCassette('apiKeys/childUserApiKeys.yml');
TestUtil::setupCassette('apiKeys/retrieveChildUserApiKeys.yml');

$user = self::$client->user->create([
'name' => 'Test User',
Expand All @@ -75,4 +60,45 @@ public function testChildUserApiKeys(): void
// Delete the user once done so we don't pollute with hundreds of child users
self::$client->user->delete($childUser->id);
}

/**
* Test retrieving all API keys.
*/
public function testAllApiKeys(): void
{
TestUtil::setupCassette('apiKeys/allApiKeys.yml');

$apiKeys = self::$client->apiKeys->all();

$this->assertContainsOnlyInstancesOf(ApiKey::class, $apiKeys['keys']);
foreach ($apiKeys['children'] as $child) {
$this->assertContainsOnlyInstancesOf(ApiKey::class, $child['keys']);
}
}

/**
* Test creating an API key for a child user.
*/
public function testApiKeyLifecycle(): void
{
TestUtil::setupCassette('apiKeys/lifecycle.yml');

// Create an API key
$referralClient = new EasyPostClient((string)getenv('REFERRAL_CUSTOMER_PROD_API_KEY'));
$apiKey = $referralClient->apiKeys->create('production');
$this->assertInstanceOf(ApiKey::class, $apiKey);
$this->assertStringMatchesFormat('ak_%s', $apiKey->id);
$this->assertEquals('production', $apiKey->mode);

// Disable the API key
$apiKey = $referralClient->apiKeys->disable($apiKey->id);
$this->assertFalse($apiKey->active);

// Enable the API key
$apiKey = $referralClient->apiKeys->enable($apiKey->id);
$this->assertTrue($apiKey->active);

// Delete the API key
$referralClient->apiKeys->delete($apiKey->id);
}
}
Loading
Loading