0Pricing
Vibe Coding · บทเรียน

การจัดการความลับและคีย์

เก็บคีย์ API ให้ปลอดภัย อย่าใส่ไว้ในพรอมป์

การจัดการความลับและคีย์ เป็นบทเรียน Vibe Coding ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vibe Coding และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Some Things Must Stay Secret

Many services give you an API key — a secret password that proves your app is allowed to use them. Payment tools, AI models, and email services all use keys.

If a key leaks, strangers can run up your bill or abuse your account. So a core vibe-coding habit is knowing how to keep secrets out of the wrong places — including out of your prompts.

What a Key Looks Like

An API key is usually a long random string. You include it with requests so the service knows it's you.

The danger: it's just text, so it's easy to accidentally paste it somewhere public. Treat a key like a house key — you wouldn't tape it to your front door.

sk_live_8f2a91c4d7e0b36a55f1c9a...
(a real key is long, random, and SECRET)

The Golden Rule: Never Hardcode Keys

The most common mistake is pasting a key directly into your code. AI sometimes does this too. If that code goes public (like on GitHub), your key is exposed to the world within minutes.

Instead, keys live in a separate, private place. Tell AI explicitly to never put the key in the code itself.

Never put my API key directly in the code. Read it
from an environment variable instead, and add the
key name to a .env file that is ignored by git.

Environment Variables

The standard safe home for secrets is an environment variable — a value stored outside your code that the app reads at runtime. Your code refers to it by name, not by the actual secret.

This snippet shows how code reads a key by name rather than containing the secret itself.

// The code only references the NAME, not the secret
const apiKey = process.env.WEATHER_API_KEY;

if (!apiKey) {
  console.log('No key found — set it in .env');
} else {
  console.log('Key loaded safely (length:', apiKey.length, ')');
}

The .env File and .gitignore

Secrets go in a file usually named .env. Crucially, you add .env to your .gitignore so it's never uploaded or shared. The code reads from it; the file stays on your machine.

Whenever you set up keys, double-check the .env file is ignored. Ask AI to confirm it for you.

Show me my .gitignore and confirm that .env is
listed in it. If it isn't, add it so my secret keys
never get committed to git.

Don't Paste Secrets into Prompts

It's tempting to paste a real key into a chat with AI so it 'just works.' Don't. Your prompts may be logged, and the key could end up in shared history.

Instead, use a placeholder. Tell AI the variable name and let it write code that reads the real value from your .env locally.

Use an environment variable called
OPENAI_API_KEY for the key. I'll add the real
value to my .env file myself — just write the code
to read it from there.

Frontend Keys Are Visible

Here's a trap: any key in code that runs in the user's browser can be seen by anyone who looks. Browser code is never truly secret.

So secret keys (like payment or AI keys) must be used from a server or serverless function, not directly in the page. Ask AI to route sensitive calls through the backend.

This AI feature needs my secret key, but the call
is currently in the browser where users could see
it. Move the request to a serverless function so
the key stays on the server and is never exposed to
the frontend.

Setting Secrets on Hosting Platforms

When you deploy to hosts like Vercel, Netlify or Replit, you don't upload your .env file. Instead, each platform has a settings page where you add environment variables securely.

Your code reads them the same way — by name. The platform keeps the actual values safe.

I'm deploying this to Vercel. Tell me exactly which
environment variables I need to add in the Vercel
dashboard, and confirm the code reads each one by
name so deployment works.

What to Do If a Key Leaks

If a key ever ends up somewhere public, don't panic — rotate it. Go to the service's dashboard, delete the old key, and generate a new one. The leaked key instantly stops working.

Then update your .env and hosting settings with the new value. Treat leaked keys as compromised, always.

A Simple Secrets Checklist

Make these habits automatic on every project:

  • Keys live in .env, never in code
  • .env is listed in .gitignore
  • Secret keys run on the server, not in the browser
  • Never paste real keys into AI prompts
  • On hosting, add keys in the dashboard, not the repo

Tell your AI tool to follow this checklist and it will set things up safely by default.

Limit What a Key Can Do

Many services let you restrict a key so a leak does less damage: cap its spending, limit it to certain domains, or give it read-only access.

Setting these limits is a smart safety net. When a service offers it, ask AI to walk you through adding usage caps and domain restrictions in the dashboard.

Walk me through adding a monthly spending limit and
a domain restriction to my API key in this
service's dashboard, so that even if the key leaks,
the damage is capped.

Quick Check

You're adding a paid AI feature that needs a secret key. Where should that key live so it stays safe?

Recap: Handling Secrets and Keys

You learned that API keys are secret passwords that must never be hardcoded or pasted into prompts. Safe keys live in environment variables via a .env file that's git-ignored, and secret keys run on the server, not in the browser.

You saw how to set secrets on hosts like Vercel, and how to rotate a leaked key. Follow the checklist and AI will set things up safely. You've now covered files, APIs, and data — the building blocks of real apps.

คำถามที่พบบ่อย

บทเรียน “การจัดการความลับและคีย์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการความลับและคีย์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vibe Coding ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vibe Coding มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการความลับและคีย์”

เก็บคีย์ API ให้ปลอดภัย อย่าใส่ไว้ในพรอมป์ คุณปฏิบัติ Vibe Coding ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vibe Coding หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vibe Coding บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการความลับและคีย์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Vibe Coding นี้ได้ไหม

ได้ บทเรียน Vibe Coding ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสื่อสารกับ API
  2. การอ่านและบันทึกไฟล์
  3. การจัดเก็บข้อมูลอย่างง่าย
  4. การจัดการความลับและคีย์
← กลับไปที่ Vibe Coding