> For the complete documentation index, see [llms.txt](https://docs.apismart.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apismart.ai/welcome-to-apismart/api-keys/secure-your-api-keys.md).

# Secure Your API Keys

Learn how to protect your ApiSmart API Keys, avoid accidental exposure, and respond quickly if a key is compromised.

Your API Key is a sensitive credential used to authenticate requests to the ApiSmart API.

Anyone with access to a valid key may be able to make API requests using the permissions available to your account. Protect your API Keys carefully and remove or disable any key that may have been exposed.

### 🔐 Keep API Keys Private

Treat every API Key like a password.

Do not:

* Share API Keys publicly
* Commit API Keys to public Git repositories
* Paste API Keys into public code samples
* Include full API Keys in screenshots
* Store API Keys in publicly accessible files
* Expose API Keys in browser-side or other untrusted client-side code
* Include full API Keys in logs or error messages

Only provide an API Key to systems and applications that require it.

### 📦 Store Keys Outside Your Source Code

Avoid hard-coding API Keys directly in your application.

Instead, use environment variables or a secure secrets management system.

For example:

```bash
export APISMART_API_KEY="your-api-key"
```

Your application can then read the value from the environment instead of storing the credential in the source code.

#### Python

```python
import os

api_key = os.getenv("APISMART_API_KEY")
```

#### JavaScript

```javascript
const apiKey = process.env.APISMART_API_KEY;
```

Make sure files containing secrets, such as `.env`, are excluded from version control.

### 🧩 Use Separate Keys for Different Environments

When possible, use separate API Keys for different applications or environments.

For example:

* Development
* Testing
* Staging
* Production

This makes it easier to identify usage and disable a specific credential without affecting every application connected to your account.

Use descriptive key names so you can quickly understand where each key is being used.

### 🖥️ Keep API Keys on Trusted Servers

For web applications, API Keys should normally remain on your backend or another trusted server environment.

Avoid embedding a secret API Key directly in:

* Browser JavaScript
* Public frontend source code
* Mobile application bundles
* Public configuration files

If users can inspect the application and retrieve the key, the credential should be considered exposed.

### 📝 Avoid Logging Full API Keys

Application logs can be accessed by developers, monitoring tools, support systems, or other services.

Do not record complete API Keys in logs.

If you need to identify which key was used, log only a safe identifier or limited prefix rather than the complete credential.

The ApiSmart Console can also help you identify and manage existing API Keys without exposing the full credential unnecessarily.

### 🔄 Review Your API Keys Regularly

Periodically review the API Keys in your account.

Check whether:

* Each key still belongs to an active application
* Key names clearly identify their purpose
* Old development or test keys are still required
* Unexpected or unused keys should be disabled or deleted

Removing unused credentials reduces unnecessary exposure.

### 🚨 If an API Key Is Exposed

If you believe an API Key has been leaked or accessed by someone who should not have it, stop using that key as soon as possible.

Recommended steps:

1. **Disable the exposed API Key**
2. **Create a replacement API Key**
3. **Update your application to use the new key**
4. **Verify that the application works with the replacement key**
5. **Delete the exposed key when it is no longer required**
6. **Review recent API usage for unexpected activity**

Do not continue using a credential that may have been compromised.

### ⚠️ Authentication Errors

If a key has been disabled, deleted, or is otherwise invalid, API requests may return:

`401 invalid_api_key`

Verify that the application is using an active API Key and that the authentication header is formatted correctly:

```http
Authorization: Bearer <your-api-key>
```

A `403 model_not_allowed` response is different from an authentication failure. It means the selected model is not currently available to the API Key or account.

For more information about API errors, see [**HTTP Status Codes and API Errors**](https://docs.apismart.ai/troubleshooting/http-status-codes-and-api-errors.md).\
For model access issues, see [**Models and Model IDs**](https://docs.apismart.ai/models-and-pricing/models-and-model-ids.md).

### ✅ API Key Security Checklist

Before deploying an application, verify that:

* Your API Key is not hard-coded into public source code
* Secret files are excluded from version control
* Production credentials are stored in a secure environment
* Development and production use separate API Keys where appropriate
* Full API Keys are not written to logs
* Public client-side applications cannot access the credential
* Unused API Keys are disabled or deleted
* Exposed keys are replaced immediately

### ➡️ Next Steps

Continue with:

* [**Create and Manage API Keys**](https://docs.apismart.ai/welcome-to-apismart/api-keys/create-and-manage-api-keys.md) — Create, edit, disable, and delete API Keys
* [**Quick Start**](https://docs.apismart.ai/welcome-to-apismart/quick-start.md) — Make your first API request
* [**API Basics**](https://docs.apismart.ai/api-guides/api-basics.md) — Learn common API authentication and request conventions
