Calling an AI service from your backend
Understand the idea
The route does not invent the summary. It sends a request and reads one field from the provider JSON.
The idea
The handler POSTs JSON to /v1/chat with a messages array and an Authorization header. The stand-in in this process speaks that shape and returns Summary: plus the text you sent. fetch is a real HTTP . The URL is 127.0.0.1 because this runner has no provider key, and lesson 20 is why that key is not pasted here. Point the same function at a real host later, and read the key from the environment. The the route returns to the client is only the content string, not the provider envelope.
What changes
| Piece | In this run | In production |
|---|---|---|
| Method and path | POST /v1/chat | The provider path you were given |
| Body | model and messages | The same shape, your model name |
| Authorization | Bearer demo | Bearer and the environment key |
| Text you keep | choices[0].message.content | The same field, after the next lessons check it |
The command
Summarize Milk
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
The mistake
Check the result
Status 200 and summary Summary: Milk. The stand-in saw the message text Milk.
Guided challenge
Prefix the model text with Summary
The stand-in returns Note: plus the prompt. Change that prefix to Summary: so the route returns Summary: Milk.
Prefix the model text with Summary
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
EXPECTED RESULT
status 200 Summary: Milk
Hints 0/2
Use all hints or make 3 failed checks (0/3).
Free exploration
Change this example and run it again. Each run is a new process. Nothing is saved.
Your variation
3s timeout · one stateless run. Node and Express use Piston when that instance accepts the job (public EMKC execute is whitelist-only; set PISTON_URL or PISTON_TOKEN). Otherwise a local Node process. MongoDB uses a fresh in-memory server per run, then it is stopped.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.