Skip to main content

1. What is API Security?

API security protects an API from unauthorized access. Without authentication:
Anyone who knows the API URL may be able to access it. With API key authentication:

2. What is an API Key?

An API key is a secret value used to identify or authorize an API request. Example:
The server checks whether the provided key is valid.

3. API Key Header

A common approach is to send the API key using an HTTP header.
Example request:
FastAPI can read the header and validate it.

4. Why Use API Keys?

API keys can be used to:
  • Protect private APIs
  • Identify clients
  • Restrict unauthorized requests
  • Control access to services
  • Provide simple authentication
API keys are useful for simple applications, but production systems may require stronger authentication mechanisms such as OAuth 2.0 or JWT.

5. Never Hardcode Real API Keys

Avoid storing real secrets directly in Python code. Bad:
Better:
Example:
The application reads the value from the environment.

6. What is Encryption?

Encryption converts readable data into an unreadable form.
Example:
Only someone with the correct key can decrypt the encrypted data.

7. Encryption at Rest

Encryption at rest protects data while it is stored. Examples:
Architecture:
When the data is needed:

8. Encryption vs Hashing

These concepts are different. For example:
Hashing:
The original password is not recovered from the hash.

9. Fernet Encryption

For a simple Python demonstration, cryptography provides Fernet symmetric encryption. Install:
Fernet uses the same secret key for encryption and decryption.

10. security_demo.py


11. Run the API

Open:
The Swagger UI can be used to test the API.

12. Setting the API Key

Windows PowerShell

Then run:

Windows CMD

Then:

13. Testing API Authentication

The protected endpoint is:
With the correct header:
Response:
With an incorrect key:
Response:
HTTP status:

14. Testing Encryption

Send:
with the API key. The API returns something similar to:
The encrypted value cannot be directly understood as the original text.

15. Encryption Flow

When the original data is required:

16. Important Security Concepts

Authentication

Determines who is allowed to access the API.

Authorization

Determines what an authenticated user is allowed to access.

Encryption

Protects data from being readable if someone gains access to the stored ciphertext.

17. API Key vs Encryption

These solve different problems.
They can be used together:

18. Basic Security Best Practices

Use HTTPS

API keys should not normally be transmitted over plain HTTP in production.

Store secrets securely

Use environment variables or a secrets manager instead of putting real keys directly in source code.

Rotate API keys

Replace keys periodically or when compromise is suspected.

Validate input

Use Pydantic models to validate API requests.

Avoid logging secrets

Do not print API keys, passwords, tokens, or decrypted confidential data in application logs.

19. Local Security Architecture


20. Main Learning

Key takeaway: API key authentication controls access to the FastAPI service, while encryption protects sensitive data when it is stored. For production systems, HTTPS, secure secret management, key rotation, proper authentication, and access controls should also be used.