0Pricing
Vibe Coding · 강의

키와 오류 처리하기

비밀 정보와 실패를 안전하게 관리해 보세요.

키와 오류 처리하기은(는) CoddyKit의 무료 Vibe Coding 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Vibe Coding 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Vibe Coding 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Keys Are Secrets

An API key proves who you are and often ties to billing. If it leaks, others can run up your bill or abuse your access. Treat it like a password.

This lesson covers storing keys safely and handling the errors that real-world calls inevitably throw, so your integrations stay secure and reliable.

Never Hardcode Keys

Pasting a key directly in your code is the most common mistake. It ends up in version control, screenshots, and shared snippets where anyone can grab it.

Instead, keep keys out of source files entirely. Ask your assistant to refactor any hardcoded key into a safer location before you commit.

I hardcoded my API key in this file. Refactor the code so the key
is read from an environment variable instead, and show me how.

Environment Variables

The standard place for keys is an environment variable, often loaded from a .env file kept out of version control. Your code reads it by name at runtime.

This keeps secrets off the page and lets each environment, local or production, use its own key without changing code.

Set up a .env file for my Node project with an API_KEY entry,
and show the code that reads it. Add .env to my .gitignore.

Keep Keys Off the Client

Keys placed in front-end browser code are visible to anyone who opens dev tools. For sensitive APIs, the call should happen on a server you control.

A small backend route holds the key, calls the API, and returns only the data. Your assistant can scaffold this proxy pattern for you.

My API key must stay secret but my app is front-end only. Create a
small backend proxy route that holds the key and calls the API for me.

Rotating a Leaked Key

If a key ever leaks, rotate it: generate a new one in the provider's dashboard and revoke the old. Do not just delete it from code, the leaked copy still works.

Assume any key that touched public code is compromised. Rotating is fast and the only safe response.

Expect Things to Fail

Networks drop, services go down, and limits get hit. Robust integrations assume failure and handle it, rather than crashing or showing a blank screen.

Wrapping calls so errors are caught and turned into friendly messages is what separates a demo from a real app.

Wrap this API call in error handling so a network failure shows the
user a friendly message instead of crashing the app.

Try, Catch, and Status Checks

In code, a try block holds the call and a catch block handles thrown errors. But fetch does not throw on a 404 or 500, so you must also check response.ok.

Checking the status code yourself catches errors the try block alone would miss. Ask your assistant to add both layers.

Add a try/catch around this fetch and also check response.ok,
throwing a clear error if the status code is not successful.

Reading Error Responses

APIs often return a JSON error body explaining what went wrong, like "invalid_api_key" or "missing parameter city". This message is more useful than the status alone.

Log and surface this body during development. Your assistant can map common error codes to fixes for the specific API you use.

This API returned a 400 with this error body. Explain what it means
and how I fix my request.

Handling Rate Limits

A 429 status means you exceeded the rate limit. The response often includes a Retry-After header telling you how long to wait before trying again.

Good clients back off and retry instead of hammering. Ask your assistant to add a retry with a delay that respects that header.

When my call returns a 429, read the Retry-After header and retry
automatically after waiting. Show the full code.

Timeouts and Fallbacks

A call that hangs forever freezes your app. Set a timeout so slow requests fail fast, then show cached data or a retry button as a fallback.

Designing for the unhappy path keeps users in control. Describe the fallback you want and let your assistant wire it up.

Add a 5-second timeout to this fetch. If it times out, show the user
a cached result and a retry button.

Putting It Together

A production-ready call hides the key on a server, checks the status, catches errors, respects rate limits, and times out gracefully. Each layer is small.

You rarely write all this by hand. You describe the requirements and your AI assistant assembles a robust, secure integration you can trust.

Quick Check

Test your understanding of keys and error handling.

Recap

Keep keys out of code in environment variables, off the client, and rotate them if leaked. Expect failures and handle them with try/catch plus status checks.

Read error bodies, back off on 429s, and add timeouts with fallbacks. With these habits and your AI assistant, your API integrations stay secure and resilient.

자주 묻는 질문

“키와 오류 처리하기” 강의는 무료인가요?

네 — “키와 오류 처리하기” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Vibe Coding 강의 전체를 잠금 해제할 수 있습니다. Vibe Coding 강의에는 총 4개의 강의가 포함되어 있습니다.

“키와 오류 처리하기”에서 뭘 배우나요?

비밀 정보와 실패를 안전하게 관리해 보세요. 브라우저에서 직접 실행하는 실습 코드로 Vibe Coding을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Vibe Coding을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Vibe Coding은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.

“키와 오류 처리하기” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Vibe Coding 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Vibe Coding 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. API란 무엇인가요?
  2. 인공지능과 함께 API 문서 읽기
  3. 첫 API 호출 만들기
  4. 키와 오류 처리하기
← Vibe Coding(으)로 돌아가기