[{"data":1,"prerenderedAt":1245,"buildId":1246},["ShallowReactive",2],{"docs:\u002Fguide\u002Fapi-routes":3},{"page":4,"section":16,"document":17,"credits":10,"prev":1237,"next":1241},{"id":5,"path":6,"file":7,"title":8,"sourceTitle":8,"description":9,"tagline":9,"eyebrow":10,"credits":10,"keywords":11,"icon":12,"layout":13,"sidebar":14,"toc":14,"kind":10,"group":10,"navigation":14,"order":15},"api-routes","\u002Fguide\u002Fapi-routes","guide\u002Fapi-routes.md","API routes","respond answers one route in the form the request picked, from one value or function per form, with your status and headers.",null,[],"i-lucide-braces","article",true,4,"guide",{"nodes":18,"toc":1228},[19,31,622,625,680,687,703,715,723,737,752,758,764,818,821,838,846,852,926,936,947,953,1011,1028,1043,1049,1055,1078,1085,1098,1120,1126,1139,1154,1160,1177,1188,1199,1210,1216],[20,21,22,26,27,30],"p",{},[23,24,25],"code",{},"respond(request, renders, init?)"," answers one route. Each key of ",[23,28,29],{},"renders"," is a form the route offers, and the first key is the default.",[32,33,37],"pre",{"language":34,"filename":35,"class":36,"wrap":14},"ts","app.ts","shiki shiki-themes github-light-default github-dark-default dark:github-dark-default",[23,38,40,63,64,63,78,63,92,63,94,63,106,63,120,63,132,63,170,63,175,63,177,63,183,63,228,63,260,63,283,63,287,63,289,63,318,63,323,63,375,63,430,63,451,63,453,63,468,63,470,63,507,63,509,63,519,63,526,63,546,63,574,63,588,63,593,63,608,63,613,63,618],{"class":39},"language-ts",[41,42,45,49,53,56,60],"span",{"class":43,"style":44},"line","display: inline",[41,46,48],{"style":47},"color:#CF222E;--shiki-dark:#FF7B72","import",[41,50,52],{"style":51},"color:#1F2328;--shiki-dark:#E6EDF3"," { H3, defineHandler } ",[41,54,55],{"style":47},"from",[41,57,59],{"style":58},"color:#0A3069;--shiki-dark:#A5D6FF"," \"h3\"",[41,61,62],{"style":51},";","\n",[41,65,66,68,71,73,76],{"class":43,"style":44},[41,67,48],{"style":47},[41,69,70],{"style":51}," { whoasks } ",[41,72,55],{"style":47},[41,74,75],{"style":58}," \"whoasks\"",[41,77,62],{"style":51},[41,79,80,82,85,87,90],{"class":43,"style":44},[41,81,48],{"style":47},[41,83,84],{"style":51}," { middleware } ",[41,86,55],{"style":47},[41,88,89],{"style":58}," \"whoasks\u002Fh3\"",[41,91,62],{"style":51},[41,93],{"class":43,"style":44},[41,95,96,99,103],{"class":43,"style":44},[41,97,98],{"style":47},"interface",[41,100,102],{"style":101},"color:#953800;--shiki-dark:#FFA657"," Result",[41,104,105],{"style":51}," {",[41,107,108,111,114,118],{"class":43,"style":44},[41,109,110],{"style":101},"  ok",[41,112,113],{"style":47},":",[41,115,117],{"style":116},"color:#0550AE;--shiki-dark:#79C0FF"," boolean",[41,119,62],{"style":51},[41,121,122,125,127,130],{"class":43,"style":44},[41,123,124],{"style":101},"  url",[41,126,113],{"style":47},[41,128,129],{"style":116}," string",[41,131,62],{"style":51},[41,133,134,137,139,142,144,146,148,151,154,156,158,160,163,165,167],{"class":43,"style":44},[41,135,136],{"style":101},"  errors",[41,138,113],{"style":47},[41,140,141],{"style":51}," { ",[41,143,23],{"style":101},[41,145,113],{"style":47},[41,147,129],{"style":116},[41,149,150],{"style":51},"; ",[41,152,153],{"style":101},"message",[41,155,113],{"style":47},[41,157,129],{"style":116},[41,159,150],{"style":51},[41,161,162],{"style":101},"fix",[41,164,113],{"style":47},[41,166,129],{"style":116},[41,168,169],{"style":51}," }[];",[41,171,172],{"class":43,"style":44},[41,173,174],{"style":51},"}",[41,176],{"class":43,"style":44},[41,178,179],{"class":43,"style":44},[41,180,182],{"style":181},"color:#6E7781;--shiki-dark:#8B949E","\u002F\u002F Stands for your own check.",[41,184,185,188,191,195,198,201,203,205,208,211,214,216,219,222,225],{"class":43,"style":44},[41,186,187],{"style":47},"async",[41,189,190],{"style":47}," function",[41,192,194],{"style":193},"color:#8250DF;--shiki-dark:#D2A8FF"," validate",[41,196,197],{"style":51},"(",[41,199,200],{"style":101},"url",[41,202,113],{"style":47},[41,204,129],{"style":116},[41,206,207],{"style":47}," |",[41,209,210],{"style":116}," null",[41,212,213],{"style":51},")",[41,215,113],{"style":47},[41,217,218],{"style":101}," Promise",[41,220,221],{"style":51},"\u003C",[41,223,224],{"style":101},"Result",[41,226,227],{"style":51},"> {",[41,229,230,233,236,239,242,245,248,251,254,257],{"class":43,"style":44},[41,231,232],{"style":47},"  const",[41,234,235],{"style":116}," errors",[41,237,238],{"style":47}," =",[41,240,241],{"style":51}," [{ code: ",[41,243,244],{"style":58},"\"E1\"",[41,246,247],{"style":51},", message: ",[41,249,250],{"style":58},"\"Missing title\"",[41,252,253],{"style":51},", fix: ",[41,255,256],{"style":58},"\"Add a \u003Ctitle>\"",[41,258,259],{"style":51}," }];",[41,261,262,265,268,271,274,277,280],{"class":43,"style":44},[41,263,264],{"style":47},"  return",[41,266,267],{"style":51}," { ok: ",[41,269,270],{"style":116},"false",[41,272,273],{"style":51},", url: url ",[41,275,276],{"style":47},"??",[41,278,279],{"style":58}," \"\"",[41,281,282],{"style":51},", errors };",[41,284,285],{"class":43,"style":44},[41,286,174],{"style":51},[41,288],{"class":43,"style":44},[41,290,291,294,297,299,302,305,307,309,311,313,315],{"class":43,"style":44},[41,292,293],{"style":47},"const",[41,295,296],{"style":193}," agentText",[41,298,238],{"style":47},[41,300,301],{"style":51}," (",[41,303,304],{"style":101},"r",[41,306,113],{"style":47},[41,308,102],{"style":101},[41,310,213],{"style":51},[41,312,113],{"style":47},[41,314,129],{"style":116},[41,316,317],{"style":47}," =>",[41,319,320],{"class":43,"style":44},[41,321,322],{"style":51},"  [",[41,324,325,328,330,333,336,339,342,345,348,350,352,355,357,360,363,365,367,369,372],{"class":43,"style":44},[41,326,327],{"style":58},"    `${",[41,329,304],{"style":51},[41,331,332],{"style":58},".",[41,334,335],{"style":51},"ok",[41,337,338],{"style":47}," ?",[41,340,341],{"style":58}," \"ok\"",[41,343,344],{"style":47}," :",[41,346,347],{"style":58}," \"failed\"} validate ${",[41,349,304],{"style":51},[41,351,332],{"style":58},[41,353,354],{"style":51},"errors",[41,356,332],{"style":58},[41,358,359],{"style":116},"length",[41,361,362],{"style":58},"} errors, ${",[41,364,304],{"style":51},[41,366,332],{"style":58},[41,368,200],{"style":51},[41,370,371],{"style":58},"}`",[41,373,374],{"style":51},",",[41,376,377,380,383,386,389,392,395,398,401,403,405,407,410,412,414,416,419,421,423,425,427],{"class":43,"style":44},[41,378,379],{"style":47},"    ...",[41,381,382],{"style":51},"r.errors.",[41,384,385],{"style":193},"map",[41,387,388],{"style":51},"((",[41,390,391],{"style":101},"e",[41,393,394],{"style":51},") ",[41,396,397],{"style":47},"=>",[41,399,400],{"style":58}," `error ${",[41,402,391],{"style":51},[41,404,332],{"style":58},[41,406,23],{"style":51},[41,408,409],{"style":58},"} ${",[41,411,391],{"style":51},[41,413,332],{"style":58},[41,415,153],{"style":51},[41,417,418],{"style":58},"}. fix: ${",[41,420,391],{"style":51},[41,422,332],{"style":58},[41,424,162],{"style":51},[41,426,371],{"style":58},[41,428,429],{"style":51},"),",[41,431,432,435,438,440,443,446,448],{"class":43,"style":44},[41,433,434],{"style":51},"  ].",[41,436,437],{"style":193},"join",[41,439,197],{"style":51},[41,441,442],{"style":58},"\"",[41,444,445],{"style":47},"\\n",[41,447,442],{"style":58},[41,449,450],{"style":51},");",[41,452],{"class":43,"style":44},[41,454,455,457,460,462,465],{"class":43,"style":44},[41,456,293],{"style":47},[41,458,459],{"style":116}," site",[41,461,238],{"style":47},[41,463,464],{"style":193}," whoasks",[41,466,467],{"style":51},"();",[41,469],{"class":43,"style":44},[41,471,472,475,478,481,483,486,488,491,493,496,499,501,504],{"class":43,"style":44},[41,473,474],{"style":47},"export",[41,476,477],{"style":47}," const",[41,479,480],{"style":116}," app",[41,482,113],{"style":47},[41,484,485],{"style":101}," H3",[41,487,238],{"style":47},[41,489,490],{"style":47}," new",[41,492,485],{"style":193},[41,494,495],{"style":51},"().",[41,497,498],{"style":193},"use",[41,500,197],{"style":51},[41,502,503],{"style":193},"middleware",[41,505,506],{"style":51},"(site));",[41,508],{"class":43,"style":44},[41,510,511,514,517],{"class":43,"style":44},[41,512,513],{"style":51},"app.",[41,515,516],{"style":193},"get",[41,518,197],{"style":51},[41,520,521,524],{"class":43,"style":44},[41,522,523],{"style":58},"  \"\u002Fapi\u002Fvalidate\"",[41,525,374],{"style":51},[41,527,528,531,533,535,537,540,542,544],{"class":43,"style":44},[41,529,530],{"style":193},"  defineHandler",[41,532,197],{"style":51},[41,534,187],{"style":47},[41,536,301],{"style":51},[41,538,539],{"style":101},"event",[41,541,394],{"style":51},[41,543,397],{"style":47},[41,545,105],{"style":51},[41,547,548,551,554,556,559,561,564,566,568,571],{"class":43,"style":44},[41,549,550],{"style":47},"    const",[41,552,553],{"style":116}," result",[41,555,238],{"style":47},[41,557,558],{"style":47}," await",[41,560,194],{"style":193},[41,562,563],{"style":51},"(event.url.searchParams.",[41,565,516],{"style":193},[41,567,197],{"style":51},[41,569,570],{"style":58},"\"url\"",[41,572,573],{"style":51},"));",[41,575,576,579,582,585],{"class":43,"style":44},[41,577,578],{"style":47},"    return",[41,580,581],{"style":51}," site.",[41,583,584],{"style":193},"respond",[41,586,587],{"style":51},"(event.req, {",[41,589,590],{"class":43,"style":44},[41,591,592],{"style":51},"      json: result,",[41,594,595,598,601,603,605],{"class":43,"style":44},[41,596,597],{"style":193},"      markdown",[41,599,600],{"style":51},": () ",[41,602,397],{"style":47},[41,604,296],{"style":193},[41,606,607],{"style":51},"(result),",[41,609,610],{"class":43,"style":44},[41,611,612],{"style":51},"    });",[41,614,615],{"class":43,"style":44},[41,616,617],{"style":51},"  }),",[41,619,620],{"class":43,"style":44},[41,621,450],{"style":51},[20,623,624],{},"Each client gets the form its request picked:",[626,627,628,643],"table",{},[629,630,631],"thead",{},[632,633,634,638],"tr",{},[635,636,637],"th",{},"Client",[635,639,640],{},[23,641,642],{},"Content-Type",[644,645,646,657,667],"tbody",{},[632,647,648,652],{},[649,650,651],"td",{},"curl, Codex",[649,653,654],{},[23,655,656],{},"application\u002Fjson",[632,658,659,662],{},[649,660,661],{},"Claude Code, Cursor",[649,663,664],{},[23,665,666],{},"text\u002Fmarkdown; charset=utf-8",[632,668,669,674],{},[649,670,671],{},[23,672,673],{},"Accept: text\u002Fplain",[649,675,676,679],{},[23,677,678],{},"text\u002Fplain; charset=utf-8",", the same text",[20,681,682,683,686],{},"For ",[23,684,685],{},"?url=https:\u002F\u002Fa.dev",", an agent gets this text:",[32,688,690],{"language":689,"class":36,"wrap":14},"text",[23,691,693,63,698],{"class":692},"language-text",[41,694,695],{"class":43,"style":44},[41,696,697],{},"failed validate 1 errors, https:\u002F\u002Fa.dev",[41,699,700],{"class":43,"style":44},[41,701,702],{},"error E1 Missing title. fix: Add a \u003Ctitle>",[20,704,705,707,708,711,712,332],{},[23,706,584],{}," works without h3 too. It takes a Web ",[23,709,710],{},"Request"," and returns a ",[23,713,714],{},"Response",[716,717,722],"h2",{"id":718,"anchor":719},"offers-and-the-default",{"label":720,"icon":721},"Section titled \"Offers and the default\"","#","Offers and the default",[20,724,725,726,729,730,729,733,736],{},"The keys are form names: ",[23,727,728],{},"html",", ",[23,731,732],{},"markdown",[23,734,735],{},"json",", and the names your layers add.",[20,738,739,740,743,744,747,748,751],{},"Key order is the order of the offers. ",[23,741,742],{},"{ json, markdown }"," answers ",[23,745,746],{},"Accept: *\u002F*"," with JSON, and ",[23,749,750],{},"{ markdown, json }"," answers it with Markdown.",[20,753,754,755,332],{},"A key that names no form throws ",[23,756,757],{},"whoasks: no form named \"yaml\"",[716,759,763],{"id":760,"anchor":761},"values",{"label":762,"icon":721},"Section titled \"Values\"","Values",[626,765,766,776],{},[629,767,768],{},[632,769,770,773],{},[635,771,772],{},"Value",[635,774,775],{},"What is sent",[644,777,778,786,799,810],{},[632,779,780,783],{},[649,781,782],{},"A string",[649,784,785],{},"The string as the body",[632,787,788,793],{},[649,789,790,791],{},"A ",[23,792,714],{},[649,794,795,796],{},"That response, with its own status and headers, plus ",[23,797,798],{},"Vary",[632,800,801,804],{},[649,802,803],{},"Anything else",[649,805,806,809],{},[23,807,808],{},"JSON.stringify"," of it",[632,811,812,815],{},[649,813,814],{},"A function",[649,816,817],{},"Its return value, by the rows above. It runs only when its form is picked",[20,819,820],{},"A function may be async. Put expensive work in a function, so a request that picks JSON does not build the Markdown.",[20,822,823,824,826,827,830,831,834,835,837],{},"For a string or a JSON value, ",[23,825,642],{}," is the picked type. A ",[23,828,829],{},"text\u002F*"," type gets ",[23,832,833],{},"; charset=utf-8",", and ",[23,836,656],{}," does not.",[20,839,790,840,842,843,845],{},[23,841,714],{}," keeps its own ",[23,844,642],{},". whoasks does not change it.",[716,847,851],{"id":848,"anchor":849},"status-and-headers",{"label":850,"icon":721},"Section titled \"Status and headers\"","Status and headers",[32,853,854],{"language":34,"class":36,"wrap":14},[23,855,856,63,867,63,872,63,899,63,922],{"class":39},[41,857,858,861,863,865],{"class":43,"style":44},[41,859,860],{"style":47},"return",[41,862,581],{"style":51},[41,864,584],{"style":193},[41,866,197],{"style":51},[41,868,869],{"class":43,"style":44},[41,870,871],{"style":51},"  request,",[41,873,874,877,879,881,883,886,889,891,894,896],{"class":43,"style":44},[41,875,876],{"style":51},"  { json: problem, ",[41,878,732],{"style":193},[41,880,600],{"style":51},[41,882,397],{"style":47},[41,884,885],{"style":58}," `failed validate. ${",[41,887,888],{"style":51},"problem",[41,890,332],{"style":58},[41,892,893],{"style":51},"title",[41,895,371],{"style":58},[41,897,898],{"style":51}," },",[41,900,901,904,907,910,913,916,919],{"class":43,"style":44},[41,902,903],{"style":51},"  { status: ",[41,905,906],{"style":116},"422",[41,908,909],{"style":51},", headers: { ",[41,911,912],{"style":58},"\"cache-control\"",[41,914,915],{"style":51},": ",[41,917,918],{"style":58},"\"no-store\"",[41,920,921],{"style":51}," } },",[41,923,924],{"class":43,"style":44},[41,925,450],{"style":51},[20,927,928,931,932,935],{},[23,929,930],{},"init"," is a ",[23,933,934],{},"ResponseInit",". Its status and headers apply to the answer in whichever form is picked.",[20,937,938,940,941,943,944,332],{},[23,939,930],{}," does not apply to a value that is a ",[23,942,714],{},". Every answer carries ",[23,945,946],{},"Vary: Accept",[716,948,952],{"id":949,"anchor":950},"html-and-derived-markdown",{"label":951,"icon":721},"Section titled \"HTML and derived Markdown\"","HTML and derived Markdown",[32,954,955],{"language":34,"class":36,"wrap":14},[23,956,957,63,968,63,1001,63,1006],{"class":39},[41,958,959,961,963,965],{"class":43,"style":44},[41,960,860],{"style":47},[41,962,581],{"style":51},[41,964,584],{"style":193},[41,966,967],{"style":51},"(request, {",[41,969,970,973,975,977,980,982,984,986,989,991,993,996,999],{"class":43,"style":44},[41,971,972],{"style":193},"  html",[41,974,600],{"style":51},[41,976,397],{"style":47},[41,978,979],{"style":58}," `\u003Cmain>\u003Ch1>${",[41,981,16],{"style":51},[41,983,332],{"style":58},[41,985,893],{"style":51},[41,987,988],{"style":58},"}\u003C\u002Fh1>\u003Cp>${",[41,990,16],{"style":51},[41,992,332],{"style":58},[41,994,995],{"style":51},"body",[41,997,998],{"style":58},"}\u003C\u002Fp>\u003C\u002Fmain>`",[41,1000,374],{"style":51},[41,1002,1003],{"class":43,"style":44},[41,1004,1005],{"style":51},"  json: guide,",[41,1007,1008],{"class":43,"style":44},[41,1009,1010],{"style":51},"});",[20,1012,1013,1014,1016,1017,1019,1020,1022,1023,1025,1026,332],{},"With an ",[23,1015,728],{}," key and no ",[23,1018,732],{}," key, ",[23,1021,584],{}," also offers ",[23,1024,732],{},", last, and makes it from the HTML with the converter. The derived Markdown keeps the status from ",[23,1027,930],{},[20,1029,1030,1031,1034,1035,1038,1039,1042],{},"With no converter (",[23,1032,1033],{},"whoasks\u002Fcore",", or ",[23,1036,1037],{},"convert: false","), nothing is derived, and ",[23,1040,1041],{},"Accept: text\u002Fmarkdown"," gets the HTML.",[20,1044,1045,1046,1048],{},"Add a ",[23,1047,732],{}," key to write the Markdown yourself. It then wins over conversion.",[716,1050,1054],{"id":1051,"anchor":1052},"refuse-with-406",{"label":1053,"icon":721},"Section titled \"Refuse with 406\"","Refuse with 406",[32,1056,1057],{"language":34,"class":36,"wrap":14},[23,1058,1059],{"class":39},[41,1060,1061,1063,1065,1067,1069,1072,1075],{"class":43,"style":44},[41,1062,293],{"style":47},[41,1064,459],{"style":116},[41,1066,238],{"style":47},[41,1068,464],{"style":193},[41,1070,1071],{"style":51},"({ strict: ",[41,1073,1074],{"style":116},"true",[41,1076,1077],{"style":51}," });",[20,1079,1080,1081,1084],{},"By default, a request that allows nothing offered gets the first key. ",[23,1082,1083],{},"Accept: image\u002Fpng"," on the route above gets JSON.",[20,1086,1087,1088,729,1091,1093,1094,1097],{},"With ",[23,1089,1090],{},"strict: true",[23,1092,584],{}," answers 406. The body is ",[23,1095,1096],{},"text\u002Fplain"," and lists the offered media types, one per line:",[32,1099,1100],{"language":689,"class":36,"wrap":14},[23,1101,1102,63,1106,63,1111,63,1116],{"class":692},[41,1103,1104],{"class":43,"style":44},[41,1105,656],{},[41,1107,1108],{"class":43,"style":44},[41,1109,1110],{},"text\u002Fmarkdown",[41,1112,1113],{"class":43,"style":44},[41,1114,1115],{},"text\u002Fx-markdown",[41,1117,1118],{"class":43,"style":44},[41,1119,1096],{},[20,1121,1122,1123,1125],{},"No function in ",[23,1124,29],{}," runs for a 406.",[20,1127,1128,1131,1132,1134,1135,1138],{},[23,1129,1130],{},"strict"," applies to ",[23,1133,584],{}," only. ",[23,1136,1137],{},"handle"," never refuses, so pages, images, and other routes behind it are unaffected.",[20,1140,1141,1142,1145,1146,1149,1150,1153],{},"A twin or ",[23,1143,1144],{},"?format="," does not bypass this. ",[23,1147,1148],{},"\u002Fapi\u002Fvalidate.md"," with ",[23,1151,1152],{},"Accept: text\u002Fhtml"," allows nothing offered, so a strict site answers it 406.",[716,1155,1159],{"id":1156,"anchor":1157},"twins-and-format-on-a-route",{"label":1158,"icon":721},"Section titled \"Twins and ?format= on a route\"","Twins and ?format= on a route",[20,1161,1162,1163,1165,1166,1168,1169,1172,1173,1176],{},"Behind ",[23,1164,1137],{}," (or the h3 middleware), ",[23,1167,1148],{}," and ",[23,1170,1171],{},"\u002Fapi\u002Fvalidate?format=md"," reach the route as ",[23,1174,1175],{},"\u002Fapi\u002Fvalidate",", and the route answers with its own Markdown.",[20,1178,1179,1180,1168,1182,1184,1185,1187],{},"A route that offers both ",[23,1181,728],{},[23,1183,732],{}," keeps its own Markdown on the twin. ",[23,1186,1137],{}," does not convert the HTML instead.",[20,1189,1190,1193,1194,915,1196,1198],{},[23,1191,1192],{},"?format=json"," works on a route that offers ",[23,1195,735],{},[23,1197,584],{}," reads it from the URL.",[20,1200,1201,1202,1204,1205,1207,1208,332],{},"A plain JSON route that does not call ",[23,1203,584],{}," is untouched by ",[23,1206,1137],{},", and gets no ",[23,1209,798],{},[716,1211,1215],{"id":1212,"anchor":1213},"other-forms",{"label":1214,"icon":721},"Section titled \"Other forms\"","Other forms",[20,1217,1218,1219,1222,1223,332],{},"A route can offer a form of your own, such as YAML or ",[23,1220,1221],{},"application\u002Fproblem+json",". See ",[1224,1225,1227],"a",{"href":1226},"\u002Fguide\u002Flayers#add-a-form","Add a form",[1229,1231,1232,1233,1234,1235,1236],{"id":718,"depth":1230,"text":722},2,{"id":760,"depth":1230,"text":763},{"id":848,"depth":1230,"text":851},{"id":949,"depth":1230,"text":952},{"id":1051,"depth":1230,"text":1054},{"id":1156,"depth":1230,"text":1159},{"id":1212,"depth":1230,"text":1215},{"path":1238,"title":1239,"description":1240},"\u002Fguide\u002Fpages","Pages","handle wraps a whole app. HTML pages become Markdown for agents, and real .md files, assets and JSON routes pass through as they are.",{"path":1242,"title":1243,"description":1244},"\u002Fguide\u002Fnegotiation","Negotiation","The seven rules that pick a form from a twin URL, ?format= and Accept, and what Claude Code, Cursor, Codex and curl get from each.",1796579201522,"78c9ee36-381e-45a5-bdf8-02819f0a7eb0"]