{"title":"Query from an application","description":"Run SQL from application code with an Altertable SDK or HTTP request, consume streamed results, and verify authentication before querying tables.","order":100,"askSuggestions":["How do I pass user input safely as query parameters?","How do I parse the streamed JSONL response?","Can I get query results as CSV or Parquet?"],"navigation":true,"body":{"code":"var Component=(()=>{var m=Object.create;var a=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var g=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var S=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports),w=(t,e)=>{for(var i in e)a(t,i,{get:e[i],enumerable:!0})},l=(t,e,i,o)=>{if(e&&typeof e==\"object\"||typeof e==\"function\")for(let r of y(e))!T.call(t,r)&&r!==i&&a(t,r,{get:()=>e[r],enumerable:!(o=f(e,r))||o.enumerable});return t};var b=(t,e,i)=>(i=t!=null?m(g(t)):{},l(e||!t||!t.__esModule?a(i,\"default\",{value:t,enumerable:!0}):i,t)),x=t=>l(a({},\"__esModule\",{value:!0}),t);var h=S((L,d)=>{d.exports=_jsx_runtime});var E={};w(E,{default:()=>p,frontmatter:()=>q});var n=b(h()),q={title:\"Query from an application\",description:\"Run SQL from application code with an Altertable SDK or HTTP request, consume streamed results, and verify authentication before querying tables.\",order:100,askSuggestions:[\"How do I pass user input safely as query parameters?\",\"How do I parse the streamed JSONL response?\",\"Can I get query results as CSV or Parquet?\"]};function u(t){let e={a:\"a\",code:\"code\",h1:\"h1\",h2:\"h2\",li:\"li\",p:\"p\",pre:\"pre\",strong:\"strong\",ul:\"ul\",...t.components},{DocsLakehouseQueryExamples:i,DocsPreviewDocument:o,DocsPreviewSection:r,DocsPrompt:c}=e;return i||s(\"DocsLakehouseQueryExamples\",!0),o||s(\"DocsPreviewDocument\",!0),r||s(\"DocsPreviewSection\",!0),c||s(\"DocsPrompt\",!0),(0,n.jsxs)(o,{anchors:\"query-from-an-application set-up-with-your-coding-agent 1-verify-authentication 2-query-your-data 3-consume-results tune-execution-when-needed troubleshooting\",children:[(0,n.jsxs)(r,{sectionId:\"query-from-an-application\",sectionTitle:\"Query from an application\",anchors:\"\",ancestors:\"\",index:\"0\",children:[(0,n.jsx)(e.h1,{id:\"query-from-an-application\",children:\"Query from an application\"}),`\n`,(0,n.jsxs)(e.p,{children:[\"Run SQL over HTTPS with an Altertable SDK or HTTP client. You need \",(0,n.jsx)(e.a,{href:\"/docs/administration/lakehouse-credentials\",children:\"lakehouse credentials\"}),\" for the environment that owns your catalogs.\"]}),`\n`,(0,n.jsxs)(e.p,{children:[\"For terminal queries, use the \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/clients/cli\",children:\"CLI\"}),\". For all request fields, response formats, and errors, use the \",(0,n.jsx)(e.a,{href:\"/docs/api-references/lakehouse#post-query\",children:\"HTTP API reference\"}),\".\"]}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"set-up-with-your-coding-agent\",sectionTitle:\"Set up with your coding agent\",anchors:\"\",ancestors:\"\",index:\"1\",children:[(0,n.jsx)(e.h2,{id:\"set-up-with-your-coding-agent\",children:\"Set up with your coding agent\"}),`\n`,(0,n.jsx)(c,{children:(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-text\",children:`Add an Altertable query to this application. Read https://altertable.ai/docs/lakehouse/query/http-api.md and the linked SDK or HTTP reference first.\n\nInspect the application runtime and existing data-access code. Ask what result the application needs and confirm the target environment. Choose an SDK supported by this runtime, or use the HTTP API.\n\nKeep lakehouse credentials on the server in the project's secret configuration. Verify authentication with SELECT 1 AS connected, then inspect the real catalog and table schemas. Use a bounded query and bind parameters for user-supplied values.\n\nIntegrate result handling and errors using the application's existing patterns. Handle the API's streamed response correctly. Run the relevant checks and verify a real result without logging credentials or sensitive rows. Summarize the code changes and required configuration.\n`})})}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"1-verify-authentication\",sectionTitle:\"1. Verify authentication\",anchors:\"\",ancestors:\"\",index:\"2\",children:[(0,n.jsx)(e.h2,{id:\"1-verify-authentication\",children:\"1. Verify authentication\"}),`\n`,(0,n.jsx)(e.p,{children:\"Set your credentials locally, then send a query that needs no existing table:\"}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-bash\",children:`export ALTERTABLE_USERNAME='{{LAKEHOUSE_USERNAME}}'\nexport ALTERTABLE_PASSWORD='{{LAKEHOUSE_PASSWORD}}'\ncurl https://api.altertable.ai/query \\\\\n  --user \"$ALTERTABLE_USERNAME:$ALTERTABLE_PASSWORD\" \\\\\n  -H 'Content-Type: application/json' \\\\\n  -d '{\"statement\":\"SELECT 1 AS connected\"}'\n`})}),`\n`,(0,n.jsxs)(e.p,{children:[\"Confirm that the response contains a \",(0,n.jsx)(e.code,{children:\"connected\"}),\" column and a row with \",(0,n.jsx)(e.code,{children:\"1\"}),\". If authentication fails, check the credentials and target environment before querying application data.\"]}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"2-query-your-data\",sectionTitle:\"2. Query your data\",anchors:\"\",ancestors:\"\",index:\"3\",children:[(0,n.jsx)(e.h2,{id:\"2-query-your-data\",children:\"2. Query your data\"}),`\n`,(0,n.jsxs)(e.p,{children:[\"See \",(0,n.jsx)(e.a,{href:\"/docs/api-references/sdks#lakehouse-sdks\",children:\"lakehouse SDK installation and availability\"}),\" before choosing a library. Rust and PHP are not yet published; use HTTP from those runtimes.\"]}),`\n`,(0,n.jsxs)(e.p,{children:[\"The cURL example below reuses \",(0,n.jsx)(e.code,{children:\"ALTERTABLE_USERNAME\"}),\" and \",(0,n.jsx)(e.code,{children:\"ALTERTABLE_PASSWORD\"}),\" from step 1. Replace the example catalog and table with names from your environment. The SDK examples show client initialization and result handling:\"]}),`\n`,(0,n.jsx)(i,{}),`\n`,(0,n.jsxs)(e.p,{children:[\"Use fully qualified table names and bound exploratory queries with \",(0,n.jsx)(e.code,{children:\"LIMIT\"}),\". In application code, store credentials in your runtime's secret configuration.\"]}),`\n`,(0,n.jsxs)(e.p,{children:[\"Use bind parameters for values supplied by users. Named parameters use \",(0,n.jsx)(e.code,{children:\"$name\"}),\"; positional parameters use \",(0,n.jsx)(e.code,{children:\"$1\"}),\", \",(0,n.jsx)(e.code,{children:\"$2\"}),\", and so on. Send an object or array in \",(0,n.jsx)(e.code,{children:\"params\"}),\" rather than interpolating values into SQL:\"]}),`\n`,(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:\"language-json\",children:`{\n  \"statement\": \"SELECT * FROM my_catalog.main.users WHERE id = $id\",\n  \"params\": { \"id\": 123 }\n}\n`})}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"3-consume-results\",sectionTitle:\"3. Consume results\",anchors:\"\",ancestors:\"\",index:\"4\",children:[(0,n.jsx)(e.h2,{id:\"3-consume-results\",children:\"3. Consume results\"}),`\n`,(0,n.jsx)(e.p,{children:\"The default HTTP response is a JSONL stream containing query metadata, a column-name array, and result rows. Process it line by line rather than parsing the whole response as one JSON document. An SDK can handle the stream for you.\"}),`\n`,(0,n.jsxs)(e.p,{children:[\"If your consumer expects a standard format, request CSV, row-oriented JSONL, or Parquet. The \",(0,n.jsx)(e.a,{href:\"/docs/api-references/lakehouse#post-query\",children:\"response format reference\"}),\" defines the exact envelopes and options.\"]}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"tune-execution-when-needed\",sectionTitle:\"Tune execution when needed\",anchors:\"\",ancestors:\"\",index:\"5\",children:[(0,n.jsx)(e.h2,{id:\"tune-execution-when-needed\",children:\"Tune execution when needed\"}),`\n`,(0,n.jsxs)(e.ul,{children:[`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.a,{href:\"/docs/lakehouse/compute/sizing\",children:\"Choose compute\"}),\" for workload sizing and session constraints.\"]}),`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.a,{href:\"/docs/lakehouse/sql-engine#transpile-another-sql-dialect\",children:\"Transpile SQL\"}),\" when porting queries from another dialect.\"]}),`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.a,{href:\"/docs/api-references/lakehouse#get-query-query-uuid\",children:\"Inspect or cancel a query\"}),\" through the query lifecycle endpoints.\"]}),`\n`]}),`\n`]}),(0,n.jsxs)(r,{sectionId:\"troubleshooting\",sectionTitle:\"Troubleshooting\",anchors:\"\",ancestors:\"\",index:\"6\",children:[(0,n.jsx)(e.h2,{id:\"troubleshooting\",children:\"Troubleshooting\"}),`\n`,(0,n.jsxs)(e.ul,{children:[`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.strong,{children:\"Unauthorized request:\"}),\" use lakehouse credentials from the intended environment.\"]}),`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.strong,{children:\"Missing table:\"}),\" qualify the table with its catalog and schema, then check catalog permissions.\"]}),`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.strong,{children:\"Unexpected response shape:\"}),\" check whether you requested the default envelope or a standard output format.\"]}),`\n`,(0,n.jsxs)(e.li,{children:[(0,n.jsx)(e.strong,{children:\"No available Worker:\"}),\" inspect compute availability and retry with backoff. See \",(0,n.jsx)(e.a,{href:\"/docs/lakehouse/compute/sizing\",children:\"compute sizing\"}),\".\"]}),`\n`]})]})]})}function p(t={}){let{wrapper:e}=t.components||{};return e?(0,n.jsx)(e,{...t,children:(0,n.jsx)(u,{...t})}):u(t)}function s(t,e){throw new Error(\"Expected \"+(e?\"component\":\"object\")+\" `\"+t+\"` to be defined: you likely forgot to import, pass, or provide it.\")}return x(E);})();\n;return Component;"},"_id":"docs/lakehouse/query/http-api.mdx","_raw":{"sourceFilePath":"docs/lakehouse/query/http-api.mdx","sourceFileName":"http-api.mdx","sourceFileDir":"docs/lakehouse/query","contentType":"mdx","flattenedPath":"docs/lakehouse/query/http-api"},"type":"Docs","path":"/lakehouse/query/http-api","level":3,"parent":"lakehouse/query"}