liveBook · Manning

API key

As explained in Build AI-Enhanced Web Apps

A user-provided API key is an API credential supplied by an individual user for requests made on that user's behalf.

An API key is a credential supplied by a user or application to authorize requests to an external AI service. User-provided keys act on the user’s behalf and require a deliberate storage and request architecture. section 9.4.1 section 9.4.3

Choose the handling model according to who owns the credential and where requests execute:

  • Store application-owned keys in private environment variables, never hardcode them, and route authenticated provider requests through server-side code. section 9.4.1 section 9.4.2
  • Encrypt user-provided keys before storing them in a secure database, treating them like passwords and using server routes as intermediaries. section 9.4.3
  • Avoid placing sensitive keys in client code or variables prefixed with NEXT_PUBLIC_; browser-visible keys can be inspected, abused, or disabled by providers. section 2.8.3 section 9.4.3
  • Monitor usage and control access with authentication, rate limiting, and provider dashboards to reduce unexpected charges and abuse. section 9.4.3 section 9.2

Server-side environment configuration

For a Next.js application, place OPENAI_API_KEY in .env.local or deployment runtime configuration, then read it from server-side code through process.env.OPENAI_API_KEY. Do not rename it with the NEXT_PUBLIC_ prefix. OpenAI integrations may require billing and available credits. section 2.8.3 section 2.7.1

Figure 9.5 OpenAI API usage dashboard displaying metrics for a GPT-4 model instance, showing API requests and total token usage. The dashboard interface includes a navigation sidebar with options for monitoring various aspects of the API deployment.

Inspect API requests and token usage through a provider usage dashboard when tracking consumption and investigating unexpected activity. section 9.4.3

How Build AI-Enhanced Web Apps covers it

How deep it goes
Distinguishes application and user keys, storage models, exposure risks, monitoring, and server intermediaries without implementing a complete credential system.
Examples
Uses .env configuration, Next.js server access, OpenAI and Gemini credentials, deployment settings, and provider usage dashboards.
What it assumes
Assumes basic Node.js and Next.js configuration knowledge while teaching credential ownership, environment visibility, and storage choices.
What you can do after
Operate/configure API credentials through private environment variables, server routes, encrypted storage, and usage monitoring.
How it connects
Connects API keys with environment variables, server routes, secrets management, authentication, rate limiting, and API usage monitoring.

Also explained in

Compare how 13 books explain API key →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.