·12 min read
ByPaula C·Kraftwire Software

AI API Security | Protecting LLM-Powered Apps, Keys, and Endpoints

Illustration for AI API Security | Protecting LLM-Powered Apps, Keys, and Endpoints

AI API security involves protecting LLM keys (OpenAI, Anthropic) and hardening the endpoints you build to interface with them. Keep provider secrets server-side, authenticate and authorize proxy requests, and apply rate limits. Treat model output as untrusted and test input and output handling for the relevant attacks.

These live figures describe all scanned sites. They do not establish prevalence for an AI-built subgroup, compare development methods, or attribute findings to AI generation.

32% of the 127 live sites SimplyScan has scanned (41 sites) have at least one high or critical security issue.

Exposed credentials and insecure API configurations are security risks.

Review who can trigger provider calls, what data they can access, and which usage controls are actually enforced. A credential or endpoint misconfiguration can expose data or permit unwanted consumption, depending on its scope.

What Does AI API Security Actually Cover?

AI API security is a multi-layered discipline that spans from your cloud provider's dashboard to the raw JavaScript running in your user's browser. It is helpful to view this through three distinct lenses:

1. Protecting Provider Credentials Keep privileged provider keys server-side, restrict who can trigger calls, and review the permissions and usage controls associated with each credential. A key’s impact depends on its actual scope.

29% of the 127 live sites SimplyScan has scanned (37 sites) have at least one medium-severity web-security issue, such as a missing Content-Security-Policy or HSTS header.

2. Hardening Application Endpoints Review authentication, authorization, input handling, and resource limits on routes such as /api/chat. Validate any caller-supplied options, including max_tokens, against server-side policy. A per-request output limit alone does not constrain aggregate usage.

10% of the 127 live sites SimplyScan has scanned (13 sites) have at least one medium-severity architecture issue, such as a library with a known vulnerability or no rate limiting.

3. Monitoring Usage and Actions Record relevant caller, operation, and usage information with appropriate privacy controls. Review tool actions separately from model text generation.

31% of the 127 live sites SimplyScan has scanned (39 sites) have at least one high-severity email-authentication issue, such as no DMARC or SPF record.

Review domain email-authentication settings separately if the application sends notifications; the email category does not measure LLM endpoint security.

Where Can Provider Credentials Be Exposed?

Keep secret credentials server-side and inspect client bundles, including .js files, and repositories for exposure. A .env filename does not make a bundled value private.

Client-side provider calls A browser fetch call to api.openai.com must not contain a privileged server API key. Inspect the actual credential used and its permissions; the destination alone does not establish secret exposure. See API keys in frontend code.

Public build configuration Names such as VITE_OPENAI_API_KEY and NEXT_PUBLIC_ANTHROPIC_KEY use public build prefixes in their respective frameworks. A value in a local .env file can become public when embedded in client output. Inspect the build and keep privileged credentials out of it; see environment variables security.

Recognizable credential patterns Patterns such as sk-, sk-ant-, and AIza can help identify candidate credentials. A prefix alone does not establish validity, privileges, or exposure impact; some Google API keys are intended for client use. Investigate the credential and its restrictions.

How to Call LLM APIs Without Exposing Your Key

Keep privileged server credentials out of browser code. A server-side proxy can hold the secret and enforce application policy. If an API supports scoped client credentials, assess that separate design against its documented permissions and lifetime.

The following Deno example illustrates a server-held key and fixed request options. Despite its original comment label, it is not a complete secure proxy: implement verifyUser, authorize usage, validate body size and prompt type before accessing prompt.length, enforce aggregate quotas, handle missing secrets and upstream errors, and propagate appropriate response status. Confirm current provider/model compatibility before adapting it.

// Example: Secure AI Proxy Pattern
Deno.serve(async (req) => {
  // 1. AUTHENTICATION: Never allow anonymous AI calls
  const authHeader = req.headers.get("Authorization");
  const user = await verifyUser(authHeader); 
  if (!user) return new Response("Unauthorized", { status: 401 });

  // 2. INPUT VALIDATION: Clamp the prompt length
  const { prompt } = await req.json();
  if (prompt.length > 2000) {
    return new Response("Prompt too long", { status: 400 });
  }

  // 3. SERVER-SIDE SECRETS: Key is fetched from the environment
  const apiKey = Deno.env.get("OPENAI_API_KEY");

  const response = await fetch("https://api.openai.com/v1/chat/completions", {
    method: "POST",
    headers: {
      "Authorization": `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      model: "gpt-4o-mini", // Hardcoded on the server
      messages: [{ role: "user", content: prompt }],
      max_tokens: 250,      // Budget is set by you, not the user
    }),
  });

  return new Response(response.body, {
    headers: { "Content-Type": "application/json" },
  });
});

A proxy can enforce several separate controls when they are implemented:

  • Model Selection: Choose allowed models server-side instead of accepting arbitrary client choices.
  • Usage Limits: A server-set max_tokens bounds supported output for a request; add aggregate limits, concurrency controls, and accounting.
  • Caller Policy: Verify identity where required and authorize the operation. Authentication alone does not establish permission to consume resources.

How Prompt Injection Leads to API Key Leaks

Prompt injection can redirect model behavior through untrusted instructions. Impact depends on the data and actions available to the application.

System Prompt Extraction Treat model-visible instructions as unsuitable for storing secrets. Outputs may reveal supplied context; a system prompt is not an access-control boundary. Keep privileged credentials outside model context.

Tool Calling and Data Exfiltration A send_email or make_web_request tool can become an exfiltration path if untrusted instructions influence its destination and accessible data. Enforce permissions, destination restrictions, and approvals appropriate to the action outside the model. Review these risks separately from the web-application categories.

Indirect Injection Untrusted instructions can arrive through retrieved webpages or documents rather than the chat input. Treat that material as data, and enforce tool permissions and data-access controls outside the model.

Hardening Your LLM-Powered Endpoints: A Checklist

Use this checklist alongside application-specific tests; see the application security checklist.

Caller Authorization

For protected features, authenticate callers with Supabase Auth or another suitable system and authorize the operation. Public features still need explicit abuse and usage controls.

Usage Limits

Enforce request, concurrency, and aggregate resource limits appropriate to the operation. Test their behavior under concurrent requests; a request-rate limit alone does not guarantee a spending bound.

Input Handling

Treat prompts and retrieved context as untrusted. Validate types and enforce request-size limits; deleting control characters or limiting length does not prevent prompt injection.

Output Handling

Use safe text rendering by default. If the application intentionally renders generated HTML, apply maintained sanitization configured for the allowed content and review URL handling. Test the complete rendering path; sanitization is not a universal XSS guarantee.

Least-Privilege Tooling

Give tools only the permissions needed for their operations. Enforce user authorization in trusted code, and do not let model output select unrestricted privileged actions.

Provider Usage Controls

Check which provider settings enforce limits and which only send alerts. Implement application-side accounting and controls as required; do not assume a dashboard budget is a hard stop.

Sensitive Context

Keep privileged API keys and passwords out of prompts. Include personal data only where authorized and necessary, with appropriate access and retention controls.

Asynchronous Verification

If using webhooks for long-running AI tasks, always verify the webhook signature.

Environment Separation

Use different API keys for development, staging, and production. Separate development and production credentials and permissions, and investigate the actual scope of any leaked key.

Regular Scanning

SimplyScan can check supported public content and fetched assets for recognizable credential exposure. Review private build artifacts and repositories through appropriate additional checks.

Monitoring and Anomaly Detection

Record usage and investigate anomalies with enough context to distinguish operational changes from abuse.

Usage Monitoring Investigate unexpected provider usage against request logs, deployment changes, and allowed user activity. A usage spike alone does not establish a leaked key or abuse.

Performance Review Measure response times, resource use, and traffic. A slowdown has multiple possible causes and does not by itself establish an attack.

80% of the 127 live sites SimplyScan has scanned (101 sites) have at least one medium-severity speed issue, such as render-blocking scripts or no route-based code splitting.

Investigate unusual traffic or spending independently of an uptime alert. See Uptime monitoring.

AI Visibility (AEO) Answer Engine Optimization (AEO) checks can help identify crawlability issues; they do not guarantee that an AI engine will find, understand, or cite a site.

Alternatives to GitHub Advanced Security for AI Apps

Compare source-code checks and deployed observations against the application’s requirements. See the GitHub Advanced Security comparison for related review considerations.

SimplyScan’s URL scan checks supported public behavior and content. Inspect deployed JavaScript as well as source configuration because a build can embed a secret in client code.

How to Audit Your AI App Today

Inspect browser requests to destinations such as api.openai.com and determine which credentials they contain. Revoke exposed privileged credentials; a direct request alone does not establish that a server secret was leaked.

The free SimplyScan URL scanner reports supported public findings, including:

  • Recognizable provider and Google API key patterns, subject to credential-specific interpretation.
  • Exposed .env content and supported Git-metadata paths.
  • Security-header configuration, including CSP and HSTS.
  • Anonymously readable sensitive backend data; this does not verify every RLS policy or authenticated operation.

A SimplyScan URL scan takes about 30 seconds with a free account required.

The free scan includes two rescans for checking changes. Review findings and test the affected behavior separately.

For developers using AI editors, the SimplyScan MCP server allows you to run these checks directly within Cursor or Windsurf.

Related Guides

Related Free Tools

FAQ

Is it safe to put an OpenAI or Anthropic API key in frontend code? Keep privileged provider API keys out of client bundles and browser requests. Store them server-side and enforce caller permissions at the proxy. If a provider supports a scoped client credential, assess that distinct credential’s permissions and lifetime; it is not equivalent to exposing a server key.

How do I know if my AI API key has leaked? Inspect source, repository history, deployed assets, and requests for credentials. Investigate unexplained usage, but do not treat a spike alone as proof of leakage. SimplyScan checks supported public inputs for recognizable secret patterns; a clean result does not establish that no key was exposed.

What should I do first after leaking an LLM API key? Revoke the exposed credential and replace it through the provider’s supported process. Remove copies from code and published artifacts, investigate usage and access, and restrict the replacement’s permissions. Check which usage controls are enforced rather than assuming alerts cap spending.

Does a spend cap protect against prompt injection? An enforced usage limit constrains the resources it covers; it does not prevent prompt injection or unauthorized data disclosure. Enforce tool permissions, data access, and output handling independently.

What is "unbounded consumption" in LLM security? It is resource consumption without adequate bounds, potentially causing service disruption or unwanted charges. Review request sizes, output limits, concurrency, aggregate quotas, and caller permissions. A per-request output limit alone does not cap total usage.

Can SimplyScan find exposed AI API keys? SimplyScan checks supported public content and fetched assets for recognizable credential patterns, including supported AI-provider signatures. Investigate each finding’s validity and scope; scanning does not establish that every key has been found.

Frequently asked questions

Keep privileged provider API keys out of client bundles and browser requests. Store them server-side and enforce caller permissions at the proxy. If a provider supports a scoped client credential, assess that distinct credential’s permissions and lifetime; it is not equivalent to exposing a server key.
Inspect source, repository history, deployed assets, and requests for credentials. Investigate unexplained usage, but do not treat a spike alone as proof of leakage. SimplyScan checks supported public inputs for recognizable secret patterns; a clean result does not establish that no key was exposed.
Revoke the exposed credential and replace it through the provider’s supported process. Remove copies from code and published artifacts, investigate usage and access, and restrict the replacement’s permissions. Check which usage controls are enforced rather than assuming alerts cap spending.
An enforced usage limit constrains the resources it covers; it does not prevent prompt injection or unauthorized data disclosure. Enforce tool permissions, data access, and output handling independently.
It is resource consumption without adequate bounds, potentially causing service disruption or unwanted charges. Review request sizes, output limits, concurrency, aggregate quotas, and caller permissions. A per-request output limit alone does not cap total usage.
SimplyScan checks supported public content and fetched assets for recognizable credential patterns, including supported AI-provider signatures. Investigate each finding’s validity and scope; scanning does not establish that every key has been found.

Is your app secure? Run a free security scan in 30 seconds · free account required.

Scan your app free →