π AppML β How to Connect API: Display External JSON in HTML Without JavaScript
π§² Introduction β Why Use APIs with AppML?
APIs are essential for dynamic applications, allowing you to fetch real-time data such as products, users, news, or weather. AppML lets you consume RESTful JSON APIs directly inside HTMLβwithout writing a single line of JavaScript.
π― In this tutorial, you’ll learn:
- How to connect external APIs using AppML
- How to bind API data to HTML elements
- How to display the data with
{{}}placeholders - How to filter or modify data using controllers
- Full code examples with detailed explanations
π§± AppML API Integration Basics
AppML uses the appml-data attribute to fetch external data via HTTP GET and render it using HTML templates.
<div appml-data="https://api.example.com/data">
<p>{{title}}</p>
</div>
π How it works:
- The URL must return a JSON array of objects.
- AppML loops through each object and replaces
{{field}}placeholders. - The repeated HTML block (here,
<p>) is generated for each item.
π§ͺ Example 1: Display User List from API
Weβll use this sample public API:
π https://jsonplaceholder.typicode.com/users
β JSON Response Sample:
[
{ "id": 1, "name": "Leanne Graham", "email": "leanne@example.com" },
{ "id": 2, "name": "Ervin Howell", "email": "ervin@example.com" }
]
β HTML Code:
<div appml-data="https://jsonplaceholder.typicode.com/users">
<h3>User Directory</h3>
<p>{{name}} β {{email}}</p>
</div>
π Code Explanation:
| Line | Description |
|---|---|
appml-data="..." | Fetches JSON data from the provided URL. |
<h3> | A static heading above the list. |
<p>{{name}} β {{email}}</p> | Repeats for each object; replaces {{name}} and {{email}} with data from the API. |
π§ͺ Example 2: Render API Data in a Table
β HTML Code:
<div appml-data="https://jsonplaceholder.typicode.com/posts">
<table border="1">
<tr>
<th>ID</th><th>Title</th>
</tr>
<tr>
<td>{{id}}</td>
<td>{{title}}</td>
</tr>
</table>
</div>
π Code Explanation:
| Line | Description |
|---|---|
appml-data="..." | Calls an API returning post data. |
<table> | A static table structure for the output. |
<th> row | Sets column headers: ID and Title. |
<tr><td>{{id}}</td><td>{{title}}</td></tr> | This row is cloned for every item in the array and filled with the actual values from the API. |
π§ͺ Example 3: Filtering API Data Using Controller
Letβs show only the first 5 posts.
β
filter-controller.js
myAppML.onshow = function() {
myAppML.filter = function(row) {
return row.id <= 5;
};
};
β HTML Code:
<div
appml-controller="filter-controller.js"
appml-data="https://jsonplaceholder.typicode.com/posts">
<p>{{id}} β {{title}}</p>
</div>
π Code Explanation:
onshowruns after data loads.myAppML.filterapplies a function to each row.- Only rows where
id <= 5are displayed. - The
<p>line is repeated for each filtered row.
π οΈ Example 4: Proxying Authenticated API via PHP
If your API requires headers (e.g., token authentication), AppML alone wonβt work. Instead, use a PHP proxy:
β
PHP Proxy: api-proxy.php
<?php
$opts = ["http" => ["header" => "Authorization: Bearer YOUR_TOKEN"]];
$context = stream_context_create($opts);
echo file_get_contents("https://api.example.com/secure-data", false, $context);
?>
β HTML:
<div appml-data="api-proxy.php">
<p>{{title}}</p>
</div>
π Code Explanation:
- PHP fetches secured API data with headers.
- AppML reads the result from the proxy script as if it were a normal JSON file.
π‘ Best Practices & Tips
| Tip | Reason |
|---|---|
| β Use CORS-friendly APIs | AppML uses AJAX, which requires CORS |
| β Ensure data is a JSON array | AppML requires top-level array for iteration |
| β Donβt use APIs with tokens directly | Expose via a backend proxy |
| β Combine with controllers | For filtering, formatting, or sorting |
π Summary β Recap & Key Takeaways
π What You Learned:
- How
appml-datafetches data from public APIs - How to bind JSON fields with
{{field}} - How to format output as paragraphs or tables
- How to use controllers for filtering API data
- How to use a PHP proxy for authenticated APIs
βοΈ AppMLβs API integration is perfect for dashboards, catalogs, and public-facing views without needing any JavaScript.
β FAQs β AppML API Usage
β Can AppML send POST or PUT requests?
β No. AppML only supports GET requests via appml-data.
β Can AppML consume nested JSON like user.name?
β οΈ Not directly. Flatten or restructure via controller or backend.
β Can I fetch data from private APIs using tokens?
β
Yes, but through a secure proxy script (e.g., PHP).
β What happens if API doesnβt return an array?
β AppML will not render anything. Use a controller or backend to transform the structure.
β Can I display images or links from API data?
β
Yes! Use <img src="{{imageUrl}}"> or <a href="{{link}}">Visit</a> inside your AppML block.
Share Now :
