Skip to content

Create API Key ๐Ÿ”’ โ€‹

Creates a new API key. The key value is automatically generated by the system.

Endpoint โ€‹

POST /api/ApiKey/create

๐Ÿ”’ JWT Required
x-api-key: <your-api-key>Authorization: Bearer <jwt-token>Content-Type: application/json

Authentication Required

๐Ÿ”’ JWT Required - Both API key and JWT token are required

Request Example โ€‹

bash
curl -X POST https://shipyo.it/api/ApiKey/create \
  -H "x-api-key: ak_1234567890abcdef" \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Mobile App API Key",
    "allowedIp": "192.168.1.200",
    "isActive": true,
    "description": "API key for mobile application integration"
  }'

Request Body โ€‹

json
{
  "name": "Mobile App API Key",
  "allowedIp": "192.168.1.200",
  "isActive": true,
  "description": "API key for mobile application integration"
}

Fields:

  • name (string, required): Human-readable name for the API key
  • allowedIp (string, optional): IP address restriction (null for no restriction)
  • isActive (boolean, required): Whether the API key should be active immediately
  • description (string, optional): Description of the API key's purpose

Success Response โ€‹

201 API Key Created Successfully

New API key has been generated with secure random value

Example Response:

{
  "success": true,
  "data": {
    "id": 3,
    "key": "ak_3456789012bcdef3456789012",
    "name": "Mobile App API Key",
    "isActive": true,
    "description": "API key for mobile application integration",
    "allowedIp": "192.168.1.200",
    "createdDate": "2024-08-25T15:30:00Z",
    "permissions": [
      "read",
      "write"
    ],
    "tenantId": 1,
    "expiresAt": null
  },
  "message": "API Key created successfully"
}

Response Fields โ€‹

FieldTypeDescriptionSecurity Notes
successbooleanAlways true for successful creationOperation status
data.idnumberUnique API key identifierUse for management operations
data.keystringACTUAL API KEY VALUEโš ๏ธ Store securely - shown only once
data.namestringHuman-readable API key nameFor identification purposes
data.isActivebooleanAPI key activation statusMust be true to use
data.descriptionstringPurpose descriptionDocumentation
data.allowedIpstring/nullIP address restrictionSecurity constraint
data.createdDatestringISO timestamp of creationAudit information
data.permissionsarrayGranted permissionsAccess scope
data.tenantIdnumberAssociated tenant IDScope limitation
data.expiresAtstring/nullExpiration datenull = no expiration
messagestringSuccess confirmationHuman-readable message

๐Ÿ”’ Critical Security Information โ€‹

API Key Value - First and Only Display โ€‹

  • ๐Ÿšจ Store Immediately: The key value is only shown once upon creation
  • ๐Ÿ” Secure Storage: Store in environment variables or secure configuration
  • โŒ Cannot Retrieve: You cannot get the full key value again later
  • ๐Ÿ”„ Regeneration: If lost, you must create a new API key

IP Address Restrictions โ€‹

  • ๐ŸŒ allowedIp: If set, API key only works from this IP address
  • ๐Ÿ”“ null value: No IP restriction (works from any IP - less secure)
  • ๐Ÿ›ก๏ธ Security recommendation: Always set IP restrictions for production

Immediate Usage โ€‹

bash
# Test the newly created API key immediately
curl -X GET https://shipyo.it/api/Token/generate-from-apikey \
  -H "x-api-key: ak_3456789012bcdef3456789012"

Post-Creation Actions โ€‹

Immediate steps required:

  1. ๐Ÿ’พ Save the key value in your application's secure configuration
  2. ๐Ÿงช Test the key with a simple API call to verify it works
  3. ๐Ÿ“ Document usage in your deployment notes
  4. ๐Ÿ” Set up monitoring for key usage patterns
  5. ๐Ÿ“… Plan rotation schedule for security best practices

Important Notes โ€‹

  • Tenant Scope: API key is automatically associated with your current tenant
  • Permissions: Inherit from your user role and tenant policies
  • Activation: Key is immediately active and ready to use
  • Management: Use the returned id for future update/delete operations
  • Audit: Key creation is logged for security monitoring

Next Steps โ€‹

  1. Store the key securely in your application
  2. Update your application to use the new API key
  3. Test all integrations with the new key
  4. Monitor usage through API logs
  5. Schedule regular rotation for enhanced security

Error Responses โ€‹

400 Bad Request

Request contains invalid data or validation failures

Example Response:

{
  "success": false,
  "message": "Validation failed",
  "errors": [
    "Name is required",
    "allowedIp must be a valid IP address"
  ]
}

Common validation errors:

  • Missing required name field
  • API key name already exists within tenant
  • Invalid IP address format in allowedIp
  • Name contains invalid characters
  • Description too long (if limits apply)

401 Unauthorized

Authentication credentials are missing or invalid

Example Response:

{
  "success": false,
  "message": "API Key is missing.",
  "errors": [
    "Missing x-api-key header"
  ]
}

Authentication issues:

  • Missing x-api-key header
  • Invalid API key value
  • Missing Authorization: Bearer <token> header
  • JWT token expired or invalid
  • Malformed authentication headers

403 Forbidden

Valid credentials but insufficient permissions to create API keys

Example Response:

{
  "success": false,
  "message": "Forbidden - Insufficient permissions",
  "errors": [
    "Cannot create API keys"
  ]
}

Permission issues:

  • User role does not allow API key creation
  • Tenant-level API key creation restrictions
  • Maximum API key limit reached for tenant
  • Cross-tenant API key creation denied

409 Conflict

API key name already exists within the tenant

Example Response:

{
  "success": false,
  "message": "API key name already exists",
  "errors": [
    "An API key with this name already exists"
  ]
}

Conflict scenarios:

  • Another API key in the same tenant has the same name
  • Case-insensitive name collision
  • Concurrent API key creation with same name

Resolution: Choose a different name or update the existing API key

422 Unprocessable Entity

Request violates business rules or system constraints

Example Response:

{
  "success": false,
  "message": "API key limit exceeded",
  "errors": [
    "Tenant has reached maximum API key limit of 10"
  ]
}

Business rule violations:

  • Tenant has reached maximum API key limit
  • IP address is in a blocked range
  • Name violates naming conventions
  • Requested permissions not available for tenant tier
  • API key creation temporarily disabled for tenant

500 Internal Server Error

Unexpected server error during API key creation

Example Response:

{
  "success": false,
  "message": "Internal server error",
  "errors": [
    "Failed to generate API key"
  ]
}

Possible causes:

  • Database connection failure
  • API key generation service unavailable
  • Cryptographic key generation errors
  • Internal workflow failures

Client action: Retry the request or contact support if persistent

Important Security Notes โ€‹

Key Generation โ€‹

  • API key values are automatically generated by the system
  • Keys follow a secure format (e.g., ak_ prefix + random string)
  • Key values are cryptographically secure and unique

First and Only Display โ€‹

  • โš ๏ธ The full API key is only shown once upon creation
  • Store the key value securely immediately after creation
  • You cannot retrieve the full key value later for security reasons

IP Restrictions โ€‹

  • Use allowedIp to restrict API key usage to specific IP addresses
  • Set to null or omit for no IP restriction (less secure)
  • IP restrictions provide an additional security layer

Best Practices โ€‹

Naming Convention โ€‹

  • Use descriptive names that indicate purpose
  • Include environment (e.g., "Production Web App", "Dev Mobile")
  • Avoid generic names like "API Key 1"

Security Recommendations โ€‹

  • Set allowedIp when possible for enhanced security
  • Create separate keys for different applications/environments
  • Set isActive: false initially if key needs approval process
  • Document the purpose in the description field

Key Management โ€‹

  • Regularly audit and rotate API keys
  • Deactivate unused keys promptly
  • Monitor key usage through logs and analytics
  • Implement key expiration policies when available

Post-Creation Steps โ€‹

After creating an API key:

  1. Save the Key: Store the key value in your application's secure configuration
  2. Test the Key: Verify the key works with a simple API call
  3. Document Usage: Record where and how the key is being used
  4. Monitor Access: Set up monitoring for key usage patterns
  5. Plan Rotation: Schedule regular key rotation for security

Example Usage After Creation โ€‹

bash
# Test the newly created API key
curl -X GET https://shipyo.it/api/User/getAll \
  -H "x-api-key: ak_3456789012bcdef3456789012" \
  -H "Authorization: Bearer your-jwt-token"