[{"data":1,"prerenderedAt":2272},["ShallowReactive",2],{"blog-en-mcp-model-context-protocol-web-dev":3,"blog-related-en-mcp-model-context-protocol-web-dev":816},{"id":4,"title":5,"author":6,"body":7,"date":800,"description":801,"extension":802,"featured":803,"image":804,"meta":805,"navigation":143,"path":806,"readingTime":170,"seo":807,"slug":808,"stem":809,"tags":810,"__hash__":815},"blog\u002Fblog\u002Fen\u002Fmcp-model-context-protocol-web-dev.md","Model Context Protocol (MCP) Explained for Web Developers: Giving Tools to AI Agents","Adam Abdel-Djamal",{"type":8,"value":9,"toc":795},"minimark",[10,14,26,29,32,37,49,59,61,65,72,682,684,688,706,712,788,791],[11,12,13],"p",{},"Until recently, connecting an LLM to your database or internal API meant writing proprietary, bespoke function-calling wrappers. Every AI client (OpenAI, Anthropic, LangChain) had a different schema format, and reusing tools across platforms required endless adapter boilerplate.",[11,15,16,17,21,22,25],{},"Enter the ",[18,19,20],"strong",{},"Model Context Protocol (MCP)",", open-sourced by Anthropic. MCP is rapidly becoming the ",[18,23,24],{},"USB-C of AI development",": an open standard that allows any AI agent to discover and invoke tools, inspect databases, and query business systems securely.",[11,27,28],{},"In this guide, we'll explain how MCP works and walk through building your first MCP server in TypeScript.",[30,31],"hr",{},[33,34,36],"h2",{"id":35},"_1-the-mcp-client-server-model","1. The MCP Client-Server Model",[11,38,39,40,44,45,48],{},"MCP standardizes how an AI Host (Claude Desktop, Cursor, Antigravity) talks to external tools through lightweight JSON-RPC over Standard I\u002FO (",[41,42,43],"code",{},"stdio",") or Server-Sent Events (",[41,46,47],{},"sse","):",[50,51,57],"pre",{"className":52,"code":54,"language":55,"meta":56},[53],"language-text","[AI Host \u002F Agent]\n        │  (JSON-RPC protocol)\n        ▼\n   [MCP Server]\n   ├── Prompts: Pre-packaged prompt workflows\n   ├── Resources: File contents, logs, documentation\n   └── Tools: Executable functions (e.g. `query_database`, `create_invoice`)\n","text","",[41,58,54],{"__ignoreMap":56},[30,60],{},[33,62,64],{"id":63},"_2-building-an-mcp-server-in-40-lines-of-typescript","2. Building an MCP Server in 40 Lines of TypeScript",[11,66,67,68,71],{},"Let's build an MCP server that exposes a database query tool to an AI agent using the official ",[41,69,70],{},"@modelcontextprotocol\u002Fsdk",":",[50,73,77],{"className":74,"code":75,"language":76,"meta":56,"style":56},"language-typescript shiki shiki-themes github-light github-dark","import { Server } from '@modelcontextprotocol\u002Fsdk\u002Fserver\u002Findex.js'\nimport { StdioServerTransport } from '@modelcontextprotocol\u002Fsdk\u002Fserver\u002Fstdio.js'\nimport { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol\u002Fsdk\u002Ftypes.js'\nimport { z } from 'zod'\n\nconst server = new Server(\n  { name: 'enterprise-crm-mcp', version: '1.0.0' },\n  { capabilities: { tools: {} } }\n)\n\n\u002F\u002F 1. Declare available tools to the AI Agent\nserver.setRequestHandler(ListToolsRequestSchema, async () => {\n  return {\n    tools: [\n      {\n        name: 'query_customer_invoices',\n        description: 'Lookup outstanding customer invoices and balance by customer ID',\n        inputSchema: {\n          type: 'object',\n          properties: {\n            customerId: { type: 'string', description: 'Customer identifier (e.g. CUST-1042)' },\n            minAmount: { type: 'number', description: 'Optional minimum invoice amount filter' }\n          },\n          required: ['customerId']\n        }\n      }\n    ]\n  }\n})\n\n\u002F\u002F 2. Handle tool execution requested by the LLM\nserver.setRequestHandler(CallToolRequestSchema, async (request) => {\n  if (request.params.name === 'query_customer_invoices') {\n    const { customerId, minAmount = 0 } = request.params.arguments as any\n    const invoices = await db.invoices.findMany({\n      where: { customerId, amount: { gte: minAmount } }\n    })\n\n    return {\n      content: [\n        {\n          type: 'text',\n          text: JSON.stringify(invoices, null, 2)\n        }\n      ]\n    }\n  }\n\n  throw new Error(`Tool ${request.params.name} not found`)\n})\n\n\u002F\u002F 3. Connect via Standard I\u002FO\nconst transport = new StdioServerTransport()\nawait server.connect(transport)\n","typescript",[41,78,79,99,112,125,138,145,168,186,192,198,203,210,234,242,248,254,266,277,283,294,300,317,334,340,352,358,364,370,376,382,387,393,419,437,475,497,503,509,514,522,528,534,544,572,577,583,589,594,599,633,638,643,649,667],{"__ignoreMap":56},[80,81,84,88,92,95],"span",{"class":82,"line":83},"line",1,[80,85,87],{"class":86},"szBVR","import",[80,89,91],{"class":90},"sVt8B"," { Server } ",[80,93,94],{"class":86},"from",[80,96,98],{"class":97},"sZZnC"," '@modelcontextprotocol\u002Fsdk\u002Fserver\u002Findex.js'\n",[80,100,102,104,107,109],{"class":82,"line":101},2,[80,103,87],{"class":86},[80,105,106],{"class":90}," { StdioServerTransport } ",[80,108,94],{"class":86},[80,110,111],{"class":97}," '@modelcontextprotocol\u002Fsdk\u002Fserver\u002Fstdio.js'\n",[80,113,115,117,120,122],{"class":82,"line":114},3,[80,116,87],{"class":86},[80,118,119],{"class":90}," { CallToolRequestSchema, ListToolsRequestSchema } ",[80,121,94],{"class":86},[80,123,124],{"class":97}," '@modelcontextprotocol\u002Fsdk\u002Ftypes.js'\n",[80,126,128,130,133,135],{"class":82,"line":127},4,[80,129,87],{"class":86},[80,131,132],{"class":90}," { z } ",[80,134,94],{"class":86},[80,136,137],{"class":97}," 'zod'\n",[80,139,141],{"class":82,"line":140},5,[80,142,144],{"emptyLinePlaceholder":143},true,"\n",[80,146,148,151,155,158,161,165],{"class":82,"line":147},6,[80,149,150],{"class":86},"const",[80,152,154],{"class":153},"sj4cs"," server",[80,156,157],{"class":86}," =",[80,159,160],{"class":86}," new",[80,162,164],{"class":163},"sScJk"," Server",[80,166,167],{"class":90},"(\n",[80,169,171,174,177,180,183],{"class":82,"line":170},7,[80,172,173],{"class":90},"  { name: ",[80,175,176],{"class":97},"'enterprise-crm-mcp'",[80,178,179],{"class":90},", version: ",[80,181,182],{"class":97},"'1.0.0'",[80,184,185],{"class":90}," },\n",[80,187,189],{"class":82,"line":188},8,[80,190,191],{"class":90},"  { capabilities: { tools: {} } }\n",[80,193,195],{"class":82,"line":194},9,[80,196,197],{"class":90},")\n",[80,199,201],{"class":82,"line":200},10,[80,202,144],{"emptyLinePlaceholder":143},[80,204,206],{"class":82,"line":205},11,[80,207,209],{"class":208},"sJ8bj","\u002F\u002F 1. Declare available tools to the AI Agent\n",[80,211,213,216,219,222,225,228,231],{"class":82,"line":212},12,[80,214,215],{"class":90},"server.",[80,217,218],{"class":163},"setRequestHandler",[80,220,221],{"class":90},"(ListToolsRequestSchema, ",[80,223,224],{"class":86},"async",[80,226,227],{"class":90}," () ",[80,229,230],{"class":86},"=>",[80,232,233],{"class":90}," {\n",[80,235,237,240],{"class":82,"line":236},13,[80,238,239],{"class":86},"  return",[80,241,233],{"class":90},[80,243,245],{"class":82,"line":244},14,[80,246,247],{"class":90},"    tools: [\n",[80,249,251],{"class":82,"line":250},15,[80,252,253],{"class":90},"      {\n",[80,255,257,260,263],{"class":82,"line":256},16,[80,258,259],{"class":90},"        name: ",[80,261,262],{"class":97},"'query_customer_invoices'",[80,264,265],{"class":90},",\n",[80,267,269,272,275],{"class":82,"line":268},17,[80,270,271],{"class":90},"        description: ",[80,273,274],{"class":97},"'Lookup outstanding customer invoices and balance by customer ID'",[80,276,265],{"class":90},[80,278,280],{"class":82,"line":279},18,[80,281,282],{"class":90},"        inputSchema: {\n",[80,284,286,289,292],{"class":82,"line":285},19,[80,287,288],{"class":90},"          type: ",[80,290,291],{"class":97},"'object'",[80,293,265],{"class":90},[80,295,297],{"class":82,"line":296},20,[80,298,299],{"class":90},"          properties: {\n",[80,301,303,306,309,312,315],{"class":82,"line":302},21,[80,304,305],{"class":90},"            customerId: { type: ",[80,307,308],{"class":97},"'string'",[80,310,311],{"class":90},", description: ",[80,313,314],{"class":97},"'Customer identifier (e.g. CUST-1042)'",[80,316,185],{"class":90},[80,318,320,323,326,328,331],{"class":82,"line":319},22,[80,321,322],{"class":90},"            minAmount: { type: ",[80,324,325],{"class":97},"'number'",[80,327,311],{"class":90},[80,329,330],{"class":97},"'Optional minimum invoice amount filter'",[80,332,333],{"class":90}," }\n",[80,335,337],{"class":82,"line":336},23,[80,338,339],{"class":90},"          },\n",[80,341,343,346,349],{"class":82,"line":342},24,[80,344,345],{"class":90},"          required: [",[80,347,348],{"class":97},"'customerId'",[80,350,351],{"class":90},"]\n",[80,353,355],{"class":82,"line":354},25,[80,356,357],{"class":90},"        }\n",[80,359,361],{"class":82,"line":360},26,[80,362,363],{"class":90},"      }\n",[80,365,367],{"class":82,"line":366},27,[80,368,369],{"class":90},"    ]\n",[80,371,373],{"class":82,"line":372},28,[80,374,375],{"class":90},"  }\n",[80,377,379],{"class":82,"line":378},29,[80,380,381],{"class":90},"})\n",[80,383,385],{"class":82,"line":384},30,[80,386,144],{"emptyLinePlaceholder":143},[80,388,390],{"class":82,"line":389},31,[80,391,392],{"class":208},"\u002F\u002F 2. Handle tool execution requested by the LLM\n",[80,394,396,398,400,403,405,408,412,415,417],{"class":82,"line":395},32,[80,397,215],{"class":90},[80,399,218],{"class":163},[80,401,402],{"class":90},"(CallToolRequestSchema, ",[80,404,224],{"class":86},[80,406,407],{"class":90}," (",[80,409,411],{"class":410},"s4XuR","request",[80,413,414],{"class":90},") ",[80,416,230],{"class":86},[80,418,233],{"class":90},[80,420,422,425,428,431,434],{"class":82,"line":421},33,[80,423,424],{"class":86},"  if",[80,426,427],{"class":90}," (request.params.name ",[80,429,430],{"class":86},"===",[80,432,433],{"class":97}," 'query_customer_invoices'",[80,435,436],{"class":90},") {\n",[80,438,440,443,446,449,452,455,457,460,463,466,469,472],{"class":82,"line":439},34,[80,441,442],{"class":86},"    const",[80,444,445],{"class":90}," { ",[80,447,448],{"class":153},"customerId",[80,450,451],{"class":90},", ",[80,453,454],{"class":153},"minAmount",[80,456,157],{"class":86},[80,458,459],{"class":153}," 0",[80,461,462],{"class":90}," } ",[80,464,465],{"class":86},"=",[80,467,468],{"class":90}," request.params.arguments ",[80,470,471],{"class":86},"as",[80,473,474],{"class":153}," any\n",[80,476,478,480,483,485,488,491,494],{"class":82,"line":477},35,[80,479,442],{"class":86},[80,481,482],{"class":153}," invoices",[80,484,157],{"class":86},[80,486,487],{"class":86}," await",[80,489,490],{"class":90}," db.invoices.",[80,492,493],{"class":163},"findMany",[80,495,496],{"class":90},"({\n",[80,498,500],{"class":82,"line":499},36,[80,501,502],{"class":90},"      where: { customerId, amount: { gte: minAmount } }\n",[80,504,506],{"class":82,"line":505},37,[80,507,508],{"class":90},"    })\n",[80,510,512],{"class":82,"line":511},38,[80,513,144],{"emptyLinePlaceholder":143},[80,515,517,520],{"class":82,"line":516},39,[80,518,519],{"class":86},"    return",[80,521,233],{"class":90},[80,523,525],{"class":82,"line":524},40,[80,526,527],{"class":90},"      content: [\n",[80,529,531],{"class":82,"line":530},41,[80,532,533],{"class":90},"        {\n",[80,535,537,539,542],{"class":82,"line":536},42,[80,538,288],{"class":90},[80,540,541],{"class":97},"'text'",[80,543,265],{"class":90},[80,545,547,550,553,556,559,562,565,567,570],{"class":82,"line":546},43,[80,548,549],{"class":90},"          text: ",[80,551,552],{"class":153},"JSON",[80,554,555],{"class":90},".",[80,557,558],{"class":163},"stringify",[80,560,561],{"class":90},"(invoices, ",[80,563,564],{"class":153},"null",[80,566,451],{"class":90},[80,568,569],{"class":153},"2",[80,571,197],{"class":90},[80,573,575],{"class":82,"line":574},44,[80,576,357],{"class":90},[80,578,580],{"class":82,"line":579},45,[80,581,582],{"class":90},"      ]\n",[80,584,586],{"class":82,"line":585},46,[80,587,588],{"class":90},"    }\n",[80,590,592],{"class":82,"line":591},47,[80,593,375],{"class":90},[80,595,597],{"class":82,"line":596},48,[80,598,144],{"emptyLinePlaceholder":143},[80,600,602,605,607,610,613,616,618,620,623,625,628,631],{"class":82,"line":601},49,[80,603,604],{"class":86},"  throw",[80,606,160],{"class":86},[80,608,609],{"class":163}," Error",[80,611,612],{"class":90},"(",[80,614,615],{"class":97},"`Tool ${",[80,617,411],{"class":90},[80,619,555],{"class":97},[80,621,622],{"class":90},"params",[80,624,555],{"class":97},[80,626,627],{"class":90},"name",[80,629,630],{"class":97},"} not found`",[80,632,197],{"class":90},[80,634,636],{"class":82,"line":635},50,[80,637,381],{"class":90},[80,639,641],{"class":82,"line":640},51,[80,642,144],{"emptyLinePlaceholder":143},[80,644,646],{"class":82,"line":645},52,[80,647,648],{"class":208},"\u002F\u002F 3. Connect via Standard I\u002FO\n",[80,650,652,654,657,659,661,664],{"class":82,"line":651},53,[80,653,150],{"class":86},[80,655,656],{"class":153}," transport",[80,658,157],{"class":86},[80,660,160],{"class":86},[80,662,663],{"class":163}," StdioServerTransport",[80,665,666],{"class":90},"()\n",[80,668,670,673,676,679],{"class":82,"line":669},54,[80,671,672],{"class":86},"await",[80,674,675],{"class":90}," server.",[80,677,678],{"class":163},"connect",[80,680,681],{"class":90},"(transport)\n",[30,683],{},[33,685,687],{"id":686},"_3-human-in-the-loop-hitl-safeguards","3. Human-in-the-Loop (HITL) Safeguards",[11,689,690,691,694,695,451,698,701,702,705],{},"When designing MCP tools that execute ",[18,692,693],{},"write or financial operations"," (e.g. ",[41,696,697],{},"send_payment_reminder",[41,699,700],{},"refund_transaction"," — as in my ",[18,703,704],{},"OpérIA"," copilot project), never execute the write immediately.",[11,707,708,709,71],{},"Instead, have the MCP tool return a ",[18,710,711],{},"Staged Action Object",[50,713,717],{"className":714,"code":715,"language":716,"meta":56,"style":56},"language-json shiki shiki-themes github-light github-dark","{\n  \"action\": \"DISPUTE_CREDIT\",\n  \"status\": \"STAGED\",\n  \"recipient\": \"client@enterprise.com\",\n  \"amount\": 450.00,\n  \"requiresApproval\": true\n}\n","json",[41,718,719,724,737,749,761,773,783],{"__ignoreMap":56},[80,720,721],{"class":82,"line":83},[80,722,723],{"class":90},"{\n",[80,725,726,729,732,735],{"class":82,"line":101},[80,727,728],{"class":153},"  \"action\"",[80,730,731],{"class":90},": ",[80,733,734],{"class":97},"\"DISPUTE_CREDIT\"",[80,736,265],{"class":90},[80,738,739,742,744,747],{"class":82,"line":114},[80,740,741],{"class":153},"  \"status\"",[80,743,731],{"class":90},[80,745,746],{"class":97},"\"STAGED\"",[80,748,265],{"class":90},[80,750,751,754,756,759],{"class":82,"line":127},[80,752,753],{"class":153},"  \"recipient\"",[80,755,731],{"class":90},[80,757,758],{"class":97},"\"client@enterprise.com\"",[80,760,265],{"class":90},[80,762,763,766,768,771],{"class":82,"line":140},[80,764,765],{"class":153},"  \"amount\"",[80,767,731],{"class":90},[80,769,770],{"class":153},"450.00",[80,772,265],{"class":90},[80,774,775,778,780],{"class":82,"line":147},[80,776,777],{"class":153},"  \"requiresApproval\"",[80,779,731],{"class":90},[80,781,782],{"class":153},"true\n",[80,784,785],{"class":82,"line":170},[80,786,787],{"class":90},"}\n",[11,789,790],{},"The operator reviews and confirms the staged payload in the UI before the system commits the transaction to production.",[792,793,794],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":56,"searchDepth":101,"depth":101,"links":796},[797,798,799],{"id":35,"depth":101,"text":36},{"id":63,"depth":101,"text":64},{"id":686,"depth":101,"text":687},"2026-09-18","MCP is one of the fastest-growing topics in developer search queries right now; establishing an early article here builds high search authority.","md",false,null,{},"\u002Fblog\u002Fen\u002Fmcp-model-context-protocol-web-dev",{"title":5,"description":801},"mcp-model-context-protocol-web-dev","blog\u002Fen\u002Fmcp-model-context-protocol-web-dev",[811,812,813,814,76],"ai","mcp","agents","python","9_DKdGQqCjXxTFjVhAmSWq3aDj2416w6DXMqHY-WECI",[817,1643],{"id":818,"title":819,"author":6,"body":820,"date":1631,"description":1632,"extension":802,"featured":803,"image":804,"meta":1633,"navigation":143,"path":1634,"readingTime":147,"seo":1635,"slug":804,"stem":1636,"tags":1637,"__hash__":1642},"blog\u002Fblog\u002Fen\u002Fapi-common-mistakes.md","6 Common API Mistakes (And How to Fix Them)",{"type":8,"value":821,"toc":1622},[822,825,827,831,842,873,894,896,900,907,985,990,1110,1112,1116,1119,1179,1182,1184,1188,1191,1324,1331,1333,1337,1340,1512,1515,1517,1521,1524,1558,1565,1567,1571,1574,1619],[11,823,824],{},"Building an API feels straightforward until your first production incident. Most bugs aren't complex — they're the same six mistakes repeated across thousands of codebases. Here's what they are and how to stop making them.",[30,826],{},[33,828,830],{"id":829},"_1-using-the-wrong-http-method","1. Using the Wrong HTTP Method",[11,832,833,834,837,838,841],{},"The most visible mistake. Using ",[41,835,836],{},"GET"," to delete a resource, or ",[41,839,840],{},"POST"," for everything because \"it works.\"",[50,843,847],{"className":844,"code":845,"language":846,"meta":56,"style":56},"language-http shiki shiki-themes github-light github-dark","# ❌ Deleting with GET — catastrophic if a browser prefetches this URL\nGET \u002Fapi\u002Fusers\u002F42\u002Fdelete\n\n# ✅ Correct\nDELETE \u002Fapi\u002Fusers\u002F42\n","http",[41,848,849,854,859,863,868],{"__ignoreMap":56},[80,850,851],{"class":82,"line":83},[80,852,853],{},"# ❌ Deleting with GET — catastrophic if a browser prefetches this URL\n",[80,855,856],{"class":82,"line":101},[80,857,858],{},"GET \u002Fapi\u002Fusers\u002F42\u002Fdelete\n",[80,860,861],{"class":82,"line":114},[80,862,144],{"emptyLinePlaceholder":143},[80,864,865],{"class":82,"line":127},[80,866,867],{},"# ✅ Correct\n",[80,869,870],{"class":82,"line":140},[80,871,872],{},"DELETE \u002Fapi\u002Fusers\u002F42\n",[11,874,875,876,878,879,881,882,885,886,889,890,893],{},"HTTP methods carry semantic meaning that clients, proxies, and caches rely on. ",[41,877,836],{}," must be idempotent and safe (no side effects). ",[41,880,840],{}," creates. ",[41,883,884],{},"PUT","\u002F",[41,887,888],{},"PATCH"," update. ",[41,891,892],{},"DELETE"," removes. Violating these conventions breaks caching, browser history, and every HTTP client that follows the spec.",[30,895],{},[33,897,899],{"id":898},"_2-wrong-status-codes","2. Wrong Status Codes",[11,901,902,903,906],{},"Returning ",[41,904,905],{},"200 OK"," for an error response is the most common mistake in junior APIs.",[50,908,910],{"className":714,"code":909,"language":716,"meta":56,"style":56},"\u002F\u002F ❌ Status 200, but it's an error\n{\n  \"success\": false,\n  \"error\": \"User not found\"\n}\n\n\u002F\u002F ✅ Status 404 — the client knows immediately without parsing the body\n{\n  \"error\": \"User not found\",\n  \"code\": \"USER_NOT_FOUND\"\n}\n",[41,911,912,917,921,933,943,947,951,956,960,971,981],{"__ignoreMap":56},[80,913,914],{"class":82,"line":83},[80,915,916],{"class":208},"\u002F\u002F ❌ Status 200, but it's an error\n",[80,918,919],{"class":82,"line":101},[80,920,723],{"class":90},[80,922,923,926,928,931],{"class":82,"line":114},[80,924,925],{"class":153},"  \"success\"",[80,927,731],{"class":90},[80,929,930],{"class":153},"false",[80,932,265],{"class":90},[80,934,935,938,940],{"class":82,"line":127},[80,936,937],{"class":153},"  \"error\"",[80,939,731],{"class":90},[80,941,942],{"class":97},"\"User not found\"\n",[80,944,945],{"class":82,"line":140},[80,946,787],{"class":90},[80,948,949],{"class":82,"line":147},[80,950,144],{"emptyLinePlaceholder":143},[80,952,953],{"class":82,"line":170},[80,954,955],{"class":208},"\u002F\u002F ✅ Status 404 — the client knows immediately without parsing the body\n",[80,957,958],{"class":82,"line":188},[80,959,723],{"class":90},[80,961,962,964,966,969],{"class":82,"line":194},[80,963,937],{"class":153},[80,965,731],{"class":90},[80,967,968],{"class":97},"\"User not found\"",[80,970,265],{"class":90},[80,972,973,976,978],{"class":82,"line":200},[80,974,975],{"class":153},"  \"code\"",[80,977,731],{"class":90},[80,979,980],{"class":97},"\"USER_NOT_FOUND\"\n",[80,982,983],{"class":82,"line":205},[80,984,787],{"class":90},[11,986,987],{},[18,988,989],{},"The codes that matter most:",[991,992,993,1006],"table",{},[994,995,996],"thead",{},[997,998,999,1003],"tr",{},[1000,1001,1002],"th",{},"Code",[1000,1004,1005],{},"When to use it",[1007,1008,1009,1020,1030,1040,1050,1060,1070,1080,1090,1100],"tbody",{},[997,1010,1011,1017],{},[1012,1013,1014],"td",{},[41,1015,1016],{},"200",[1012,1018,1019],{},"Success with a body",[997,1021,1022,1027],{},[1012,1023,1024],{},[41,1025,1026],{},"201",[1012,1028,1029],{},"Resource created (POST)",[997,1031,1032,1037],{},[1012,1033,1034],{},[41,1035,1036],{},"204",[1012,1038,1039],{},"Success, no body (DELETE)",[997,1041,1042,1047],{},[1012,1043,1044],{},[41,1045,1046],{},"400",[1012,1048,1049],{},"Invalid input from the client",[997,1051,1052,1057],{},[1012,1053,1054],{},[41,1055,1056],{},"401",[1012,1058,1059],{},"Not authenticated",[997,1061,1062,1067],{},[1012,1063,1064],{},[41,1065,1066],{},"403",[1012,1068,1069],{},"Authenticated but not authorized",[997,1071,1072,1077],{},[1012,1073,1074],{},[41,1075,1076],{},"404",[1012,1078,1079],{},"Resource doesn't exist",[997,1081,1082,1087],{},[1012,1083,1084],{},[41,1085,1086],{},"422",[1012,1088,1089],{},"Input syntactically valid but semantically wrong",[997,1091,1092,1097],{},[1012,1093,1094],{},[41,1095,1096],{},"429",[1012,1098,1099],{},"Rate limit exceeded",[997,1101,1102,1107],{},[1012,1103,1104],{},[41,1105,1106],{},"500",[1012,1108,1109],{},"Server error (never the client's fault)",[30,1111],{},[33,1113,1115],{"id":1114},"_3-trusting-client-input","3. Trusting Client Input",[11,1117,1118],{},"Every value sent by a client is a potential attack vector.",[50,1120,1124],{"className":1121,"code":1122,"language":1123,"meta":56,"style":56},"language-php shiki shiki-themes github-light github-dark","\u002F\u002F ❌ Never do this\n$id = $_GET['id'];\n$query = \"SELECT * FROM users WHERE id = $id\";\n\u002F\u002F SQL injection: ?id=1 OR 1=1\n\n\u002F\u002F ✅ Always validate and sanitize\n$id = filter_var($_GET['id'], FILTER_VALIDATE_INT);\nif (!$id) {\n    return response()->json(['error' => 'Invalid ID'], 400);\n}\n$user = User::findOrFail($id); \u002F\u002F parameterized query\n","php",[41,1125,1126,1131,1136,1141,1146,1150,1155,1160,1165,1170,1174],{"__ignoreMap":56},[80,1127,1128],{"class":82,"line":83},[80,1129,1130],{},"\u002F\u002F ❌ Never do this\n",[80,1132,1133],{"class":82,"line":101},[80,1134,1135],{},"$id = $_GET['id'];\n",[80,1137,1138],{"class":82,"line":114},[80,1139,1140],{},"$query = \"SELECT * FROM users WHERE id = $id\";\n",[80,1142,1143],{"class":82,"line":127},[80,1144,1145],{},"\u002F\u002F SQL injection: ?id=1 OR 1=1\n",[80,1147,1148],{"class":82,"line":140},[80,1149,144],{"emptyLinePlaceholder":143},[80,1151,1152],{"class":82,"line":147},[80,1153,1154],{},"\u002F\u002F ✅ Always validate and sanitize\n",[80,1156,1157],{"class":82,"line":170},[80,1158,1159],{},"$id = filter_var($_GET['id'], FILTER_VALIDATE_INT);\n",[80,1161,1162],{"class":82,"line":188},[80,1163,1164],{},"if (!$id) {\n",[80,1166,1167],{"class":82,"line":194},[80,1168,1169],{},"    return response()->json(['error' => 'Invalid ID'], 400);\n",[80,1171,1172],{"class":82,"line":200},[80,1173,787],{},[80,1175,1176],{"class":82,"line":205},[80,1177,1178],{},"$user = User::findOrFail($id); \u002F\u002F parameterized query\n",[11,1180,1181],{},"Validate: type, format, length, range. Sanitize before any database or filesystem operation. Never trust what arrives in the request body, headers, or query string.",[30,1183],{},[33,1185,1187],{"id":1186},"_4-leaking-sensitive-data","4. Leaking Sensitive Data",[11,1189,1190],{},"Returning the full database object without filtering is an invitation to a data breach.",[50,1192,1194],{"className":714,"code":1193,"language":716,"meta":56,"style":56},"\u002F\u002F ❌ Returning the raw User model\n{\n  \"id\": 42,\n  \"email\": \"user@example.com\",\n  \"password\": \"$2b$12$...\",\n  \"stripe_customer_id\": \"cus_xxx\",\n  \"internal_notes\": \"flagged for review\",\n  \"created_at\": \"2024-01-01\"\n}\n\n\u002F\u002F ✅ Return only what the client actually needs\n{\n  \"id\": 42,\n  \"email\": \"user@example.com\",\n  \"created_at\": \"2024-01-01\"\n}\n",[41,1195,1196,1201,1205,1217,1229,1241,1253,1265,1275,1279,1283,1288,1292,1302,1312,1320],{"__ignoreMap":56},[80,1197,1198],{"class":82,"line":83},[80,1199,1200],{"class":208},"\u002F\u002F ❌ Returning the raw User model\n",[80,1202,1203],{"class":82,"line":101},[80,1204,723],{"class":90},[80,1206,1207,1210,1212,1215],{"class":82,"line":114},[80,1208,1209],{"class":153},"  \"id\"",[80,1211,731],{"class":90},[80,1213,1214],{"class":153},"42",[80,1216,265],{"class":90},[80,1218,1219,1222,1224,1227],{"class":82,"line":127},[80,1220,1221],{"class":153},"  \"email\"",[80,1223,731],{"class":90},[80,1225,1226],{"class":97},"\"user@example.com\"",[80,1228,265],{"class":90},[80,1230,1231,1234,1236,1239],{"class":82,"line":140},[80,1232,1233],{"class":153},"  \"password\"",[80,1235,731],{"class":90},[80,1237,1238],{"class":97},"\"$2b$12$...\"",[80,1240,265],{"class":90},[80,1242,1243,1246,1248,1251],{"class":82,"line":147},[80,1244,1245],{"class":153},"  \"stripe_customer_id\"",[80,1247,731],{"class":90},[80,1249,1250],{"class":97},"\"cus_xxx\"",[80,1252,265],{"class":90},[80,1254,1255,1258,1260,1263],{"class":82,"line":170},[80,1256,1257],{"class":153},"  \"internal_notes\"",[80,1259,731],{"class":90},[80,1261,1262],{"class":97},"\"flagged for review\"",[80,1264,265],{"class":90},[80,1266,1267,1270,1272],{"class":82,"line":188},[80,1268,1269],{"class":153},"  \"created_at\"",[80,1271,731],{"class":90},[80,1273,1274],{"class":97},"\"2024-01-01\"\n",[80,1276,1277],{"class":82,"line":194},[80,1278,787],{"class":90},[80,1280,1281],{"class":82,"line":200},[80,1282,144],{"emptyLinePlaceholder":143},[80,1284,1285],{"class":82,"line":205},[80,1286,1287],{"class":208},"\u002F\u002F ✅ Return only what the client actually needs\n",[80,1289,1290],{"class":82,"line":212},[80,1291,723],{"class":90},[80,1293,1294,1296,1298,1300],{"class":82,"line":236},[80,1295,1209],{"class":153},[80,1297,731],{"class":90},[80,1299,1214],{"class":153},[80,1301,265],{"class":90},[80,1303,1304,1306,1308,1310],{"class":82,"line":244},[80,1305,1221],{"class":153},[80,1307,731],{"class":90},[80,1309,1226],{"class":97},[80,1311,265],{"class":90},[80,1313,1314,1316,1318],{"class":82,"line":250},[80,1315,1269],{"class":153},[80,1317,731],{"class":90},[80,1319,1274],{"class":97},[80,1321,1322],{"class":82,"line":256},[80,1323,787],{"class":90},[11,1325,1326,1327,1330],{},"Use serializers, DTOs, or API Resources (Laravel) to explicitly define what gets exposed. The rule: ",[18,1328,1329],{},"opt-in, not opt-out."," Start with nothing, add what's needed.",[30,1332],{},[33,1334,1336],{"id":1335},"_5-inconsistent-error-format","5. Inconsistent Error Format",[11,1338,1339],{},"When every endpoint returns errors differently, every client has to handle every format.",[50,1341,1343],{"className":714,"code":1342,"language":716,"meta":56,"style":56},"\u002F\u002F ❌ Endpoint A\n{ \"message\": \"Not found\" }\n\n\u002F\u002F ❌ Endpoint B\n{ \"error\": true, \"msg\": \"Validation failed\", \"fields\": [\"email\"] }\n\n\u002F\u002F ❌ Endpoint C\n\"Internal server error\"\n\n\u002F\u002F ✅ One format, everywhere\n{\n  \"error\": {\n    \"code\": \"VALIDATION_FAILED\",\n    \"message\": \"The request contains invalid fields.\",\n    \"details\": [\n      { \"field\": \"email\", \"issue\": \"Invalid email format\" }\n    ]\n  }\n}\n",[41,1344,1345,1350,1365,1369,1374,1410,1414,1419,1424,1428,1433,1437,1444,1456,1468,1476,1500,1504,1508],{"__ignoreMap":56},[80,1346,1347],{"class":82,"line":83},[80,1348,1349],{"class":208},"\u002F\u002F ❌ Endpoint A\n",[80,1351,1352,1355,1358,1360,1363],{"class":82,"line":101},[80,1353,1354],{"class":90},"{ ",[80,1356,1357],{"class":153},"\"message\"",[80,1359,731],{"class":90},[80,1361,1362],{"class":97},"\"Not found\"",[80,1364,333],{"class":90},[80,1366,1367],{"class":82,"line":114},[80,1368,144],{"emptyLinePlaceholder":143},[80,1370,1371],{"class":82,"line":127},[80,1372,1373],{"class":208},"\u002F\u002F ❌ Endpoint B\n",[80,1375,1376,1378,1381,1383,1386,1388,1391,1393,1396,1398,1401,1404,1407],{"class":82,"line":140},[80,1377,1354],{"class":90},[80,1379,1380],{"class":153},"\"error\"",[80,1382,731],{"class":90},[80,1384,1385],{"class":153},"true",[80,1387,451],{"class":90},[80,1389,1390],{"class":153},"\"msg\"",[80,1392,731],{"class":90},[80,1394,1395],{"class":97},"\"Validation failed\"",[80,1397,451],{"class":90},[80,1399,1400],{"class":153},"\"fields\"",[80,1402,1403],{"class":90},": [",[80,1405,1406],{"class":97},"\"email\"",[80,1408,1409],{"class":90},"] }\n",[80,1411,1412],{"class":82,"line":147},[80,1413,144],{"emptyLinePlaceholder":143},[80,1415,1416],{"class":82,"line":170},[80,1417,1418],{"class":208},"\u002F\u002F ❌ Endpoint C\n",[80,1420,1421],{"class":82,"line":188},[80,1422,1423],{"class":97},"\"Internal server error\"\n",[80,1425,1426],{"class":82,"line":194},[80,1427,144],{"emptyLinePlaceholder":143},[80,1429,1430],{"class":82,"line":200},[80,1431,1432],{"class":208},"\u002F\u002F ✅ One format, everywhere\n",[80,1434,1435],{"class":82,"line":205},[80,1436,723],{"class":90},[80,1438,1439,1441],{"class":82,"line":212},[80,1440,937],{"class":153},[80,1442,1443],{"class":90},": {\n",[80,1445,1446,1449,1451,1454],{"class":82,"line":236},[80,1447,1448],{"class":153},"    \"code\"",[80,1450,731],{"class":90},[80,1452,1453],{"class":97},"\"VALIDATION_FAILED\"",[80,1455,265],{"class":90},[80,1457,1458,1461,1463,1466],{"class":82,"line":244},[80,1459,1460],{"class":153},"    \"message\"",[80,1462,731],{"class":90},[80,1464,1465],{"class":97},"\"The request contains invalid fields.\"",[80,1467,265],{"class":90},[80,1469,1470,1473],{"class":82,"line":250},[80,1471,1472],{"class":153},"    \"details\"",[80,1474,1475],{"class":90},": [\n",[80,1477,1478,1481,1484,1486,1488,1490,1493,1495,1498],{"class":82,"line":256},[80,1479,1480],{"class":90},"      { ",[80,1482,1483],{"class":153},"\"field\"",[80,1485,731],{"class":90},[80,1487,1406],{"class":97},[80,1489,451],{"class":90},[80,1491,1492],{"class":153},"\"issue\"",[80,1494,731],{"class":90},[80,1496,1497],{"class":97},"\"Invalid email format\"",[80,1499,333],{"class":90},[80,1501,1502],{"class":82,"line":268},[80,1503,369],{"class":90},[80,1505,1506],{"class":82,"line":279},[80,1507,375],{"class":90},[80,1509,1510],{"class":82,"line":285},[80,1511,787],{"class":90},[11,1513,1514],{},"Define your error envelope once. Apply it everywhere via a global exception handler. Clients — including your own frontend — will thank you.",[30,1516],{},[33,1518,1520],{"id":1519},"_6-no-api-versioning","6. No API Versioning",[11,1522,1523],{},"Changing an endpoint response breaks every client using it. Without versioning, you can never evolve the API.",[50,1525,1527],{"className":844,"code":1526,"language":846,"meta":56,"style":56},"# ❌ No version — any breaking change affects all clients immediately\nGET \u002Fapi\u002Fusers\n\n# ✅ Versioned — v1 stays stable, v2 introduces breaking changes safely\nGET \u002Fapi\u002Fv1\u002Fusers\nGET \u002Fapi\u002Fv2\u002Fusers\n",[41,1528,1529,1534,1539,1543,1548,1553],{"__ignoreMap":56},[80,1530,1531],{"class":82,"line":83},[80,1532,1533],{},"# ❌ No version — any breaking change affects all clients immediately\n",[80,1535,1536],{"class":82,"line":101},[80,1537,1538],{},"GET \u002Fapi\u002Fusers\n",[80,1540,1541],{"class":82,"line":114},[80,1542,144],{"emptyLinePlaceholder":143},[80,1544,1545],{"class":82,"line":127},[80,1546,1547],{},"# ✅ Versioned — v1 stays stable, v2 introduces breaking changes safely\n",[80,1549,1550],{"class":82,"line":140},[80,1551,1552],{},"GET \u002Fapi\u002Fv1\u002Fusers\n",[80,1554,1555],{"class":82,"line":147},[80,1556,1557],{},"GET \u002Fapi\u002Fv2\u002Fusers\n",[11,1559,1560,1561,1564],{},"Version from day one, even if you only have v1. The cost of adding ",[41,1562,1563],{},"\u002Fv1\u002F"," later — after clients are in production — is much higher than the cost of including it from the start.",[30,1566],{},[33,1568,1570],{"id":1569},"the-takeaway","The Takeaway",[11,1572,1573],{},"None of these mistakes require advanced knowledge to fix. They require discipline: a checklist before every endpoint ships.",[1575,1576,1579,1589,1595,1601,1607,1613],"ul",{"className":1577},[1578],"contains-task-list",[1580,1581,1584,1588],"li",{"className":1582},[1583],"task-list-item",[1585,1586],"input",{"disabled":143,"type":1587},"checkbox"," HTTP method matches the operation's semantics",[1580,1590,1592,1594],{"className":1591},[1583],[1585,1593],{"disabled":143,"type":1587}," Status code is accurate",[1580,1596,1598,1600],{"className":1597},[1583],[1585,1599],{"disabled":143,"type":1587}," All input is validated before use",[1580,1602,1604,1606],{"className":1603},[1583],[1585,1605],{"disabled":143,"type":1587}," Response only exposes necessary fields",[1580,1608,1610,1612],{"className":1609},[1583],[1585,1611],{"disabled":143,"type":1587}," Errors follow the global format",[1580,1614,1616,1618],{"className":1615},[1583],[1585,1617],{"disabled":143,"type":1587}," URL includes an API version",[792,1620,1621],{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}",{"title":56,"searchDepth":101,"depth":101,"links":1623},[1624,1625,1626,1627,1628,1629,1630],{"id":829,"depth":101,"text":830},{"id":898,"depth":101,"text":899},{"id":1114,"depth":101,"text":1115},{"id":1186,"depth":101,"text":1187},{"id":1335,"depth":101,"text":1336},{"id":1519,"depth":101,"text":1520},{"id":1569,"depth":101,"text":1570},"2026-06-04","From wrong HTTP methods to leaking sensitive data — the errors most developers make when building their first API, and the concrete fixes.",{},"\u002Fblog\u002Fen\u002Fapi-common-mistakes",{"title":819,"description":1632},"blog\u002Fen\u002Fapi-common-mistakes",[1638,1639,1640,1641],"api","backend","rest","best-practices","z2NMprz3c9WpXKotgDMzEF9WM_LLLolyFqidBZu4I6s",{"id":1644,"title":1645,"author":6,"body":1646,"date":2260,"description":2261,"extension":802,"featured":803,"image":804,"meta":2262,"navigation":143,"path":2263,"readingTime":147,"seo":2264,"slug":2265,"stem":2266,"tags":2267,"__hash__":2271},"blog\u002Fblog\u002Fen\u002Fapi-rate-limiting-redis.md","API Rate Limiting in Practice: Token Bucket vs. Sliding Window with Redis",{"type":8,"value":1647,"toc":2251},[1648,1651,1654,1665,1667,1671,1676,1683,1687,1690,1698,1702,1705,1707,1711,1718,2179,2181,2185,2188,2245,2248],[11,1649,1650],{},"Without API rate limiting, a single rogue script or misconfigured webhook client can fire 10,000 requests per second at your login or checkout endpoint, exhausting your database connection pool and taking your entire application down.",[11,1652,1653],{},"Rate limiting is the first line of defense against bot attacks, credential stuffing, and API resource abuse. But which algorithm should you implement?",[11,1655,1656,1657,1660,1661,1664],{},"In this article, we compare the two industry-standard algorithms — ",[18,1658,1659],{},"Token Bucket"," and ",[18,1662,1663],{},"Sliding Window Log"," — and show how to implement a high-speed sliding window rate limiter in Redis.",[30,1666],{},[33,1668,1670],{"id":1669},"_1-comparing-the-algorithms","1. Comparing the Algorithms",[1672,1673,1675],"h3",{"id":1674},"a-fixed-window-counter-the-flawed-default","A. Fixed Window Counter (The Flawed Default)",[11,1677,1678,1679,1682],{},"Divides time into fixed buckets (e.g. 10:00 to 10:01). If a user can make 100 requests per minute, they can send 100 requests at 10:00:59, and another 100 requests at 10:01:01. That means ",[18,1680,1681],{},"200 requests in 2 seconds"," — double the intended burst limit!",[1672,1684,1686],{"id":1685},"b-token-bucket","B. Token Bucket",[11,1688,1689],{},"A bucket holds a fixed number of tokens (burst capacity). Tokens are continuously replenished at a constant rate. Each incoming request consumes one token.",[1575,1691,1692],{},[1580,1693,1694,1697],{},[18,1695,1696],{},"Great for",": APIs that want to allow legitimate temporary traffic spikes while capping sustained throughput.",[1672,1699,1701],{"id":1700},"c-sliding-window-counter-the-production-standard","C. Sliding Window Counter (The Production Standard)",[11,1703,1704],{},"Combines the low memory usage of fixed windows with the smoothness of a rolling window. It calculates the request rate by weighting the request count of the previous window and the current window.",[30,1706],{},[33,1708,1710],{"id":1709},"_2-sliding-window-rate-limiter-with-redis-sorted-sets","2. Sliding Window Rate Limiter with Redis Sorted Sets",[11,1712,1713,1714,1717],{},"Redis Sorted Sets (",[41,1715,1716],{},"ZSET",") allow us to track requests by timestamp with microsecond accuracy:",[50,1719,1721],{"className":74,"code":1720,"language":76,"meta":56,"style":56},"import Redis from 'ioredis'\n\nconst redis = new Redis(process.env.REDIS_URL!)\n\nexport async function checkRateLimit(identifier: string, limit = 60, windowInSeconds = 60) {\n  const now = Date.now()\n  const windowStart = now - (windowInSeconds * 1000)\n  const key = `ratelimit:${identifier}`\n\n  \u002F\u002F Atomic Redis Pipeline (Runs in a single round-trip)\n  const multi = redis.multi()\n  \u002F\u002F 1. Remove all request timestamps older than the active window\n  multi.zremrangebyscore(key, 0, windowStart)\n  \u002F\u002F 2. Count requests currently in the active window\n  multi.zcard(key)\n  \u002F\u002F 3. Add the current timestamp as a new request\n  multi.zadd(key, now, `${now}-${Math.random()}`)\n  \u002F\u002F 4. Set key expiration to auto-clean idle users\n  multi.expire(key, windowInSeconds)\n\n  const results = await multi.exec()\n  const currentCount = (results?.[1]?.[1] as number) || 0\n\n  if (currentCount >= limit) {\n    return {\n      allowed: false,\n      remaining: 0,\n      retryAfter: Math.ceil(windowInSeconds - ((now - windowStart) \u002F 1000))\n    }\n  }\n\n  return {\n    allowed: true,\n    remaining: limit - currentCount - 1,\n    retryAfter: 0\n  }\n}\n",[41,1722,1723,1735,1739,1764,1768,1813,1831,1857,1874,1878,1883,1900,1905,1922,1927,1937,1942,1976,1981,1991,1995,2014,2050,2054,2067,2073,2082,2091,2119,2123,2127,2131,2137,2146,2163,2171,2175],{"__ignoreMap":56},[80,1724,1725,1727,1730,1732],{"class":82,"line":83},[80,1726,87],{"class":86},[80,1728,1729],{"class":90}," Redis ",[80,1731,94],{"class":86},[80,1733,1734],{"class":97}," 'ioredis'\n",[80,1736,1737],{"class":82,"line":101},[80,1738,144],{"emptyLinePlaceholder":143},[80,1740,1741,1743,1746,1748,1750,1753,1756,1759,1762],{"class":82,"line":114},[80,1742,150],{"class":86},[80,1744,1745],{"class":153}," redis",[80,1747,157],{"class":86},[80,1749,160],{"class":86},[80,1751,1752],{"class":163}," Redis",[80,1754,1755],{"class":90},"(process.env.",[80,1757,1758],{"class":153},"REDIS_URL",[80,1760,1761],{"class":86},"!",[80,1763,197],{"class":90},[80,1765,1766],{"class":82,"line":127},[80,1767,144],{"emptyLinePlaceholder":143},[80,1769,1770,1773,1776,1779,1782,1784,1787,1789,1792,1794,1797,1799,1802,1804,1807,1809,1811],{"class":82,"line":140},[80,1771,1772],{"class":86},"export",[80,1774,1775],{"class":86}," async",[80,1777,1778],{"class":86}," function",[80,1780,1781],{"class":163}," checkRateLimit",[80,1783,612],{"class":90},[80,1785,1786],{"class":410},"identifier",[80,1788,71],{"class":86},[80,1790,1791],{"class":153}," string",[80,1793,451],{"class":90},[80,1795,1796],{"class":410},"limit",[80,1798,157],{"class":86},[80,1800,1801],{"class":153}," 60",[80,1803,451],{"class":90},[80,1805,1806],{"class":410},"windowInSeconds",[80,1808,157],{"class":86},[80,1810,1801],{"class":153},[80,1812,436],{"class":90},[80,1814,1815,1818,1821,1823,1826,1829],{"class":82,"line":147},[80,1816,1817],{"class":86},"  const",[80,1819,1820],{"class":153}," now",[80,1822,157],{"class":86},[80,1824,1825],{"class":90}," Date.",[80,1827,1828],{"class":163},"now",[80,1830,666],{"class":90},[80,1832,1833,1835,1838,1840,1843,1846,1849,1852,1855],{"class":82,"line":170},[80,1834,1817],{"class":86},[80,1836,1837],{"class":153}," windowStart",[80,1839,157],{"class":86},[80,1841,1842],{"class":90}," now ",[80,1844,1845],{"class":86},"-",[80,1847,1848],{"class":90}," (windowInSeconds ",[80,1850,1851],{"class":86},"*",[80,1853,1854],{"class":153}," 1000",[80,1856,197],{"class":90},[80,1858,1859,1861,1864,1866,1869,1871],{"class":82,"line":188},[80,1860,1817],{"class":86},[80,1862,1863],{"class":153}," key",[80,1865,157],{"class":86},[80,1867,1868],{"class":97}," `ratelimit:${",[80,1870,1786],{"class":90},[80,1872,1873],{"class":97},"}`\n",[80,1875,1876],{"class":82,"line":194},[80,1877,144],{"emptyLinePlaceholder":143},[80,1879,1880],{"class":82,"line":200},[80,1881,1882],{"class":208},"  \u002F\u002F Atomic Redis Pipeline (Runs in a single round-trip)\n",[80,1884,1885,1887,1890,1892,1895,1898],{"class":82,"line":205},[80,1886,1817],{"class":86},[80,1888,1889],{"class":153}," multi",[80,1891,157],{"class":86},[80,1893,1894],{"class":90}," redis.",[80,1896,1897],{"class":163},"multi",[80,1899,666],{"class":90},[80,1901,1902],{"class":82,"line":212},[80,1903,1904],{"class":208},"  \u002F\u002F 1. Remove all request timestamps older than the active window\n",[80,1906,1907,1910,1913,1916,1919],{"class":82,"line":236},[80,1908,1909],{"class":90},"  multi.",[80,1911,1912],{"class":163},"zremrangebyscore",[80,1914,1915],{"class":90},"(key, ",[80,1917,1918],{"class":153},"0",[80,1920,1921],{"class":90},", windowStart)\n",[80,1923,1924],{"class":82,"line":244},[80,1925,1926],{"class":208},"  \u002F\u002F 2. Count requests currently in the active window\n",[80,1928,1929,1931,1934],{"class":82,"line":250},[80,1930,1909],{"class":90},[80,1932,1933],{"class":163},"zcard",[80,1935,1936],{"class":90},"(key)\n",[80,1938,1939],{"class":82,"line":256},[80,1940,1941],{"class":208},"  \u002F\u002F 3. Add the current timestamp as a new request\n",[80,1943,1944,1946,1949,1952,1955,1957,1960,1963,1965,1968,1971,1974],{"class":82,"line":268},[80,1945,1909],{"class":90},[80,1947,1948],{"class":163},"zadd",[80,1950,1951],{"class":90},"(key, now, ",[80,1953,1954],{"class":97},"`${",[80,1956,1828],{"class":90},[80,1958,1959],{"class":97},"}-${",[80,1961,1962],{"class":90},"Math",[80,1964,555],{"class":97},[80,1966,1967],{"class":163},"random",[80,1969,1970],{"class":97},"()",[80,1972,1973],{"class":97},"}`",[80,1975,197],{"class":90},[80,1977,1978],{"class":82,"line":279},[80,1979,1980],{"class":208},"  \u002F\u002F 4. Set key expiration to auto-clean idle users\n",[80,1982,1983,1985,1988],{"class":82,"line":285},[80,1984,1909],{"class":90},[80,1986,1987],{"class":163},"expire",[80,1989,1990],{"class":90},"(key, windowInSeconds)\n",[80,1992,1993],{"class":82,"line":296},[80,1994,144],{"emptyLinePlaceholder":143},[80,1996,1997,1999,2002,2004,2006,2009,2012],{"class":82,"line":302},[80,1998,1817],{"class":86},[80,2000,2001],{"class":153}," results",[80,2003,157],{"class":86},[80,2005,487],{"class":86},[80,2007,2008],{"class":90}," multi.",[80,2010,2011],{"class":163},"exec",[80,2013,666],{"class":90},[80,2015,2016,2018,2021,2023,2026,2029,2032,2034,2037,2039,2042,2044,2047],{"class":82,"line":319},[80,2017,1817],{"class":86},[80,2019,2020],{"class":153}," currentCount",[80,2022,157],{"class":86},[80,2024,2025],{"class":90}," (results?.[",[80,2027,2028],{"class":153},"1",[80,2030,2031],{"class":90},"]?.[",[80,2033,2028],{"class":153},[80,2035,2036],{"class":90},"] ",[80,2038,471],{"class":86},[80,2040,2041],{"class":153}," number",[80,2043,414],{"class":90},[80,2045,2046],{"class":86},"||",[80,2048,2049],{"class":153}," 0\n",[80,2051,2052],{"class":82,"line":336},[80,2053,144],{"emptyLinePlaceholder":143},[80,2055,2056,2058,2061,2064],{"class":82,"line":342},[80,2057,424],{"class":86},[80,2059,2060],{"class":90}," (currentCount ",[80,2062,2063],{"class":86},">=",[80,2065,2066],{"class":90}," limit) {\n",[80,2068,2069,2071],{"class":82,"line":354},[80,2070,519],{"class":86},[80,2072,233],{"class":90},[80,2074,2075,2078,2080],{"class":82,"line":360},[80,2076,2077],{"class":90},"      allowed: ",[80,2079,930],{"class":153},[80,2081,265],{"class":90},[80,2083,2084,2087,2089],{"class":82,"line":366},[80,2085,2086],{"class":90},"      remaining: ",[80,2088,1918],{"class":153},[80,2090,265],{"class":90},[80,2092,2093,2096,2099,2102,2104,2107,2109,2112,2114,2116],{"class":82,"line":372},[80,2094,2095],{"class":90},"      retryAfter: Math.",[80,2097,2098],{"class":163},"ceil",[80,2100,2101],{"class":90},"(windowInSeconds ",[80,2103,1845],{"class":86},[80,2105,2106],{"class":90}," ((now ",[80,2108,1845],{"class":86},[80,2110,2111],{"class":90}," windowStart) ",[80,2113,885],{"class":86},[80,2115,1854],{"class":153},[80,2117,2118],{"class":90},"))\n",[80,2120,2121],{"class":82,"line":378},[80,2122,588],{"class":90},[80,2124,2125],{"class":82,"line":384},[80,2126,375],{"class":90},[80,2128,2129],{"class":82,"line":389},[80,2130,144],{"emptyLinePlaceholder":143},[80,2132,2133,2135],{"class":82,"line":395},[80,2134,239],{"class":86},[80,2136,233],{"class":90},[80,2138,2139,2142,2144],{"class":82,"line":421},[80,2140,2141],{"class":90},"    allowed: ",[80,2143,1385],{"class":153},[80,2145,265],{"class":90},[80,2147,2148,2151,2153,2156,2158,2161],{"class":82,"line":439},[80,2149,2150],{"class":90},"    remaining: limit ",[80,2152,1845],{"class":86},[80,2154,2155],{"class":90}," currentCount ",[80,2157,1845],{"class":86},[80,2159,2160],{"class":153}," 1",[80,2162,265],{"class":90},[80,2164,2165,2168],{"class":82,"line":477},[80,2166,2167],{"class":90},"    retryAfter: ",[80,2169,2170],{"class":153},"0\n",[80,2172,2173],{"class":82,"line":499},[80,2174,375],{"class":90},[80,2176,2177],{"class":82,"line":505},[80,2178,787],{"class":90},[30,2180],{},[33,2182,2184],{"id":2183},"_3-returning-standard-http-rate-limit-headers","3. Returning Standard HTTP Rate Limit Headers",[11,2186,2187],{},"Whenever your server answers a request, communicate rate limit states in the response headers (following IETF draft standards):",[50,2189,2191],{"className":844,"code":2190,"language":846,"meta":56,"style":56},"HTTP\u002F1.1 429 Too Many Requests\nRetry-After: 32\nX-RateLimit-Limit: 60\nX-RateLimit-Remaining: 0\nX-RateLimit-Reset: 1773489240\nContent-Type: application\u002Fjson\n\n{\n  \"error\": \"Too Many Requests\",\n  \"message\": \"Rate limit exceeded. Please retry in 32 seconds.\"\n}\n",[41,2192,2193,2198,2203,2208,2213,2218,2223,2227,2231,2236,2241],{"__ignoreMap":56},[80,2194,2195],{"class":82,"line":83},[80,2196,2197],{},"HTTP\u002F1.1 429 Too Many Requests\n",[80,2199,2200],{"class":82,"line":101},[80,2201,2202],{},"Retry-After: 32\n",[80,2204,2205],{"class":82,"line":114},[80,2206,2207],{},"X-RateLimit-Limit: 60\n",[80,2209,2210],{"class":82,"line":127},[80,2211,2212],{},"X-RateLimit-Remaining: 0\n",[80,2214,2215],{"class":82,"line":140},[80,2216,2217],{},"X-RateLimit-Reset: 1773489240\n",[80,2219,2220],{"class":82,"line":147},[80,2221,2222],{},"Content-Type: application\u002Fjson\n",[80,2224,2225],{"class":82,"line":170},[80,2226,144],{"emptyLinePlaceholder":143},[80,2228,2229],{"class":82,"line":188},[80,2230,723],{},[80,2232,2233],{"class":82,"line":194},[80,2234,2235],{},"  \"error\": \"Too Many Requests\",\n",[80,2237,2238],{"class":82,"line":200},[80,2239,2240],{},"  \"message\": \"Rate limit exceeded. Please retry in 32 seconds.\"\n",[80,2242,2243],{"class":82,"line":205},[80,2244,787],{},[11,2246,2247],{},"This ensures honest third-party API clients and mobile SDKs can automatically back off and retry without overwhelming your infrastructure.",[792,2249,2250],{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sScJk, html code.shiki .sScJk{--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}",{"title":56,"searchDepth":101,"depth":101,"links":2252},[2253,2258,2259],{"id":1669,"depth":101,"text":1670,"children":2254},[2255,2256,2257],{"id":1674,"depth":114,"text":1675},{"id":1685,"depth":114,"text":1686},{"id":1700,"depth":114,"text":1701},{"id":1709,"depth":101,"text":1710},{"id":2183,"depth":101,"text":2184},"2026-09-14","Comparing rate-limiting algorithms to protect your backend against scrapers, credential stuffing, and bot spam with minimal latency overhead.",{},"\u002Fblog\u002Fen\u002Fapi-rate-limiting-redis",{"title":1645,"description":2261},"api-rate-limiting-redis","blog\u002Fen\u002Fapi-rate-limiting-redis",[1638,2268,2269,1639,2270],"security","redis","performance","KXTsuQOwj895dkbCDObhyO_wOncRKtB2ufJbjc-AUyc",1792236100006]