Streaming Responses
Last updated
Last updated
Streaming responses are supported in both calling external APIs, and setting up your Xano APIs to deliver streaming responses.
You can use the Streaming External API Request endpoint to call an API that returns a stream in almost exactly the same way as you call normal APIs from your function stacks. The only difference is the structure of data returned, which would typically be an array of items that you would leverage a For Each loop to work with.
When delivering certain types of API responses, you may want to 'stream' this response (similar to your favorite AI-powered chatbots). In Xano, this is possible with a simple combination of a For Each loop and a Streaming API Response function.
We'll be using a poem in the public domain to demonstrate the streaming response. You can copy the sample below and use it in your own function stacks to test.
Data that you use for a streaming response needs to be separated into logical pieces. In this example, each item in the array is a new line in the poem. It would typically make the most sense to build a streaming response against an array just for ease of implementation.
You can now test your streaming API, and should see each item in the array streamed as part of the response.
Please note that your front-end must support streaming responses. If it does not, the response can still be delivered traditionally.
Using Run & Debug will not display a stream, and only the entire response once the stream has completed.
Create a new request with type HTTP
Paste your API endpoint URL in the URL input, and click Send.
You will see your API response delivered in the result panel.
Create a new request with type Event Stream
Paste your API endpoint URL in the URL input, and click Connect
You will see your API response delivered in the result panel.
Set the API response type to 'streaming' from the API settings, or choose the streaming option when creating a new API endpoint.
In your function stack, once you have the data you want to stream ready to go, use a For Each loop to start looping against each item in your array.
Inside of the loop, use a Streaming API Response function to deliver each item inside of the array as the loop iterates through it.