API keys for server-side identity access should stay only in a trusted backend process, never in a mobile app bundle or other client-side code. That keeps privileged API access out of user-controlled environments and reduces exposure if the app is reverse engineered. Store the secret in server-side configuration or environment variables, then use it only from backend code that already sits behind your trust boundary.
Why API keys belong only in trusted server-side code
For server-side Swift apps, the key rule is that an API key is a bearer secret, not a harmless configuration value. If the key is present in a client bundle, anyone who extracts the app can reuse it and impersonate your backend. Keep the key in server-only configuration, load it at runtime, and limit the code path that can read it.
That matters most when the identity API can read, create, or revoke sensitive identity data. In that case, the key is effectively a machine credential for your backend process, so the trust boundary must stay on the server side rather than in user-controlled code.
When teams need a refresher on secret handling patterns, the API Key Management Guide covers the lifecycle view, including storage, scoping, rotation, and revocation, while the Ultimate Guide to NHIs explains why API keys are part of broader machine identity and secret governance.
How backend Swift should store and use the key
Use a backend-only source such as environment variables, secret manager injection, or server configuration owned by the deployment platform. The Swift process should read the key at startup or on demand, then pass it only to the identity API client code that needs it. Avoid logging it, serialising it into responses, or copying it into shared app state where unrelated code can reach it.
For service-to-service access, the key should also be scoped as narrowly as the API allows. If the identity platform supports tenant, app, or endpoint restrictions, use them so a stolen key does not become a universal control plane credential. The NHI Authentication Guide is a useful reference when the question becomes how backend workloads and services should authenticate more safely than with a long-lived shared secret.
If your environment supports stronger alternatives, prefer them over a static key. Short-lived tokens, workload identity, or mTLS can reduce blast radius, but the key point for Swift is unchanged: the credential must remain inside the trusted backend process, not inside code distributed to users.
Where teams usually get this wrong
The most common failure is treating a mobile app, desktop client, or front-end shell as if it were a secure secret store. Even if the app is obfuscated, the key is still extractable once it ships to an uncontrolled environment. Another common mistake is reusing one key across too many services, which turns a single leak into broad identity-system exposure.
That exposure is not theoretical. Secret leakage in client code is a recurring pattern, and leaked API keys are often the first step in credential abuse, automated scraping, or unwanted administrative actions. The practical issue is not only disclosure, but also the scope of what the exposed key can do before you notice.
The Guide to the Secret Sprawl Challenge is directly relevant here because it shows how hard-coded credentials and CI/CD exposure create avoidable leakage paths, and the iOS apps leaking hard-coded secrets research is a concrete reminder that secrets embedded in client-delivered code are routinely recoverable.
Risk and Threat Considerations
Once an API key leaves the backend trust boundary, it becomes reusable by anyone who finds it. For identity APIs, that can mean account enumeration, unauthorized reads, abusive provisioning, or changes to identity records that should only come from trusted automation.
Failure mechanism: The key is embedded in client-side code, config shipped to users, or a reverse-engineerable binary, then reused as a valid bearer credential from outside the intended server environment.
Impact: Attackers can impersonate your application, exhaust API limits, expose identity data, or perform privileged actions until the key is revoked and replaced.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP Non-Human Identity Top 10 addresses the attack surface, NIST SP 800-53 Rev 5 and CIS Controls v8 set the technical controls, and ISO/IEC 27001:2022 defines the regulatory obligations.
| Framework | Control / Reference | Relevance |
|---|---|---|
| NIST SP 800-53 Rev 5 | IA-5 — Authenticator Management | API keys are credentials that need storage, rotation, and revocation. |
| IA-9 — Service Identification and Authentication | Server-to-API calls from Swift are service authentications, not user logins. | |
| AC-6 — Least Privilege | Identity API keys should be scoped to the minimum permitted actions. | |
| Recommendation — Manage API keys as authenticators with defined issuance, rotation, and revocation processes. Use service-to-service authentication controls instead of exposing shared keys to clients. Limit each API key to the smallest set of identity operations required. | ||
| ISO/IEC 27001:2022 | A.5.15 — Access control | The key controls access to identity APIs and must be restricted to trusted backend code. |
| Recommendation — Restrict secret access to approved backend processes and deployment roles. | ||
| CIS Controls v8 | CIS-5 — Account Management | API keys function like managed credentials that require lifecycle control. |
| Recommendation — Inventory, restrict, rotate, and revoke API keys through an owned credential process. | ||
| OWASP Non-Human Identity Top 10 | NHI-02 — Secret Leakage | Client-side exposure of API keys is a direct secret leakage problem. |
| NHI-07 — Long-Lived Secrets | Static API keys create persistent exposure if they are reused or shipped to clients. | |
| NHI-05 — Overprivileged NHI | A broadly scoped identity API key can overreach the backend's intended actions. | |
| Recommendation — Keep API keys out of client code and detect leaked secrets before release. Prefer short-lived or tightly rotated credentials over static long-lived API keys. Scope each key to the minimum API permissions needed for the backend workflow. | ||
Practitioner Guidance
What to prioritise: Treat every identity API key as a production secret with explicit ownership, scope, and rotation responsibility. If the key can access anything beyond read-only test data, it should be handled as a high-value server-side credential.
What to verify: Confirm that the key never appears in the Swift client bundle, logs, crash reports, analytics payloads, or test fixtures. Also verify that the backend process can access only the minimum identity API surface it actually needs.
Practitioner takeaway: For server-side Swift, the security decision is simple: if a secret can authorize identity operations, it belongs in backend-controlled runtime only, and any wider distribution should be treated as a credential design flaw.
Related resources from NHI Mgmt Group
- How should security teams handle API keys and tokens as part of identity governance?
- How should security teams handle OAuth tokens in multi-API applications?
- How should security teams handle scoped API keys for scripts and AI agents?
- How do security teams know when to move from API keys to workload identity?