Posted in

How to integrate third – party APIs in Fuse apps?

Alright, let’s cut to the chase—if you’re building Fuse apps, odds are you’re gonna need to pull in data or features from third-party tools at some point. Maybe you’re adding a payment gateway, integrating a weather API for a travel app, or pulling user reviews from a social platform. As someone who’s been in the Fuse game for years, I’ve seen so many devs struggle with this part: it feels clunky at first, but once you get the hang of it, it’s not that bad. Let me walk you through exactly how to do this right, from start to finish, no fancy jargon, just real talk. Fuse

First off, let’s get one thing straight: Fuse’s framework is actually set up to handle third-party APIs way better than a lot of people give it credit for. A lot of new devs think they need to jump through hoops, but nope—Fuse uses JavaScript (and TypeScript if you’re into that) for the logic layer, so working with APIs is pretty straightforward. The main thing you gotta remember is Fuse has two parts: the UX markup (that’s the .ux files where you build your UI) and the JavaScript layer (the .js files that handle all the behind-the-scenes stuff). APIs live mostly in that logic layer, so that’s where we’ll focus most of our time.

Let’s start with the prep work, ‘cause you don’t wanna dive into writing code without getting your API sorted first. Whatever third-party service you’re using—say it’s Stripe for payments, or OpenWeatherMap for weather data—you’re gonna need an API key. That’s non-negotiable. Every API has its own process for getting this, usually you sign up for an account on their site, create a project, and they’ll give you a secret key (some of them have public keys too, for client-side stuff). Here’s a pro tip I’ve learned the hard way: never hardcode your API key directly into your Fuse app’s code. If you do that, anyone can decompile your app, grab the key, and misuse it. Instead, use environment variables. Fuse makes this easy if you’re working with a team or just keeping things organized. You can create a .env file in your project root, add something like API_KEY=your-secret-key-here, and then access it in your JS code with process.env.API_KEY. Just make sure you add .env to your .gitignore so you don’t accidentally push it to GitHub—trust me, I’ve seen so many people mess that up.

Next step: choosing how to make the API calls. In the JavaScript world, fetch() is the standard now, right? And guess what? Fuse supports fetch() natively, no extra plugins needed. That’s a huge win. Some older devs might swear by Axios, but unless you’re already using it in a big project, fetch() is totally sufficient for most Fuse apps. Let’s run through a simple example to make this concrete. Say we’re building a Fuse app that shows the current weather for a user’s city, so we’re using the OpenWeatherMap API. First, we’d set up our .env file with OPEN_WEATHER_KEY=our-api-key. Then, in our JS logic file (let’s call it weather.js), we’d write a function to fetch the data.

Wait, but hold on—Fuse has a thing called observables, right? Those are how you make data reactive in your app, so when the API data comes back, the UI updates automatically. You can’t just fetch data and log it; you need to bind it to an observable. Let’s put that together. So in weather.js, first import Observable from ‘fuse-core’ (that’s Fuse’s core library, you get that for free when you start a Fuse project). Then create an observable for the weather data, and one for any errors, ‘cause APIs go down sometimes. Here’s what that code would look like:

const weatherData = Observable({
temp: null,
description: ”,
city: ”
})

const weatherError = Observable(”)

async function fetchWeather(city) {
try {
// Clear old error first
weatherError.value = ”
// Make the API call with fetch
const response = await fetch(https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${process.env.OPEN_WEATHER_KEY}&units=metric)
// Check if the response is okay (status code 200-299)
if (!response.ok) throw new Error(‘City not found or API error’)
// Parse the JSON data
const data = await response.json()
// Update the observable with the new data
weatherData.value = {
temp: Math.round(data.main.temp),
description: data.weather[0].description,
city: data.name
}
} catch (err) {
// If something goes wrong, set the error observable
weatherError.value = err.message
}
}

That’s it for the logic! Now, how do we hook this up to our UX markup? In the .ux file, we can import the observables and the fetchWeather function, then bind them directly. Let’s see a simple UI: