Fetch data from an API

APIs allow you to connect your toddle application to a backend. toddle works with any backend and has many advanced features, such as response streaming, custom redirect rules, and server fetching.

If you are new to working with APIs, check our blog post about REST APIs

Setting up a new API

To connect to a backend, you must create a new API. You can do this from the data panel on any page or component.

To connect to a backend, you must create a new API. You can do this from the data panel on any page or component. Toddle connects to any service with a REST API

From the Panel, you can now paste in a URL for the backend you wish to call or select one of our default templates.

๐Ÿ’ก If you are just starting out, try one of our demo APIs

In this case, we chose the Weather API demo template. This template creates an API in your current page or component.

When turned on, the API will automatically fetch data when the page or component is loaded and every time any of the data used to specify the request changes.

Let's take a look at the different parts of the API panel:

โ‘  The method used for the request. You can use any standard HTTP method.

โ‘ก The name of the API. New APIs will be given a default name, but you can always change it to something more meaningful to you

โ‘ข In the tabs section, you can configure the request to match the backend you call. You can set the base URL, path, and query parameters. To learn more, check out our guide to URLs here.

You can also set custom
headers and the request body from the tabs section if the request method supports it. Learn more about setting the request body below.

โ‘ฃ At the bottom of the panel, you can see the latest response returned from the server. If the API is configured to "Auto Fetch" (See below), then the API will automatically fetch the data when the page or component loads. For APIs where "Auto Fetch" is turned off, you can manually trigger the request with the โ–ถ๏ธ ("Play") button

โ‘ค Auto fetch is a powerful feature in toddle that lets each API automatically decide when to fetch data.

When turned on, the API will automatically fetch the data when the page or component is loaded and every time any of the data used to specify the request changes. For example, if a variable is used to specify part of the URL, then the API will automatically fetch data again when the value of that variable changes.

You can all so dynamically toggle "Auto fetch" using a formula.

Almost every part of the request data can set to a dynamic value using a formula.

Setting the request Body

For some types of requests, you can specify a request body.

By default, the body will be encoded as JSON unless another encoding is specified with the Content-type header. Read more about API bodies in the toddle docs.

โ‘  You use the request method to accept an HTTP body such as POST, PUT, or PATCH.

โ‘ก You must select the Body tab to view or edit the request body.

โ‘ข Using the formula editor, you can specify what data you wish to send in the body of the API request.

By default, the body will be encoded as JSON unless another encoding is specified with the Content-type header.

Accessing API data

Once you have created your API, you can access the data from any formula on your page or component.

Once you have created your API, you can access the data from any formula on your page or component. It's easier than ever to connect to any API with toddle.

Stop scrolling. Start building.

toddle is a visual web app builder that rivals custom code โ€” but accessible to your entire team!

Try toddle โ€” it's free!

ยฉ Copyright 2024 toddle. All rights reserved.