Api help

Ive been trying to see if i can make my own "Api's" to use for snap tho im running into an issue. when i use the url block its just returning me the code i have on the page not the page value. if anyone has anything that could help please lmk. https://batteryapi1.glitch.me/

    // Override the page content with JSON when the battery data is ready
    navigator.getBattery().then(battery => {
        const data = {
            batteryLevel: battery.level * 100,
            charging: battery.charging
        };
        // Set MIME type and write JSON (not standard, but may help some tools)
        document.write(JSON.stringify(data));
        document.close();
    });

I see
// Override the page content with JSON when the battery data is ready navigator.getBattery().then(battery => { const data = { batteryLevel: battery.level * 100, charging: battery.charging }; // Set MIME type and write JSON (not standard, but may help some tools) document.write(JSON.stringify(data)); document.close(); }); in snap, but


in the actual website. probably because it uses document.write though, which replaces the webpage data. I don't think there's anyway around this unless you turn on js, and in that case why use the url [ block at all.

Your issue is that the url block just gets what the browser gets before it renders or runs any javascript. You have to have the page be json from the start, aka, have the server send the json instead of html.

If you can't run code on the backend (not in the html file), then I suggest you don't try to make an api. If you can, you can try to search up "how to make a REST api" instead of asking here, since this forum is about snap, not any other language.

i made a node js at a point to put it as json but the problem was the snap block reported too early also i am doing it here because my code works its a matter of how the url block works

The snap blocks isn't returning too early, your site is just giving the browser the wrong thing.

You need to create the json on the backend instead of the frontend.

i did with nodejs. how do i stoop the block from returning anything before i get the information

const express = require("express");
const app = express();

// Endpoint for Snap-like functionality
app.get("/api/snap", (req, res) => {
// Wait for 10 seconds before creating the response data
setTimeout(() => {
// Create the JSON data
const responseData = { result: true };

// Set the correct headers for JSON response
res.setHeader("Content-Type", "application/json");

// Send the response after the delay
res.end(JSON.stringify(responseData));

}, 1000); // 10-second delay
});

// Root route to prevent "Cannot GET /"
app.get("/", (req, res) => {
res.send("Welcome to the Snap API! Use /api/snap to see the delay.");
});

// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(API is live on port ${port});
});

this is something i have been trying to see if i can get everything loaded before returning something but fore some reason it waits but still reports empty

Do you want to create a hosted Snap instance, WWW back-end powered by Snap
or something else?
And not untitled script pic - 2025-01-30T010603.475 ?

? im trying to create an api with the node js i dont want any js blocks in snap

const express = require("express");
const cors = require("cors"); // Import the cors package
const app = express();

// Enable CORS for all routes
app.use(cors());

// Endpoint for Snap-like functionality
app.get("/api/snap", (req, res) => {
// Wait for 1 second before sending the response
setTimeout(() => {
// Send plain text response
res.setHeader("Content-Type", "text/plain");
res.send("true"); // Send "true" as plain text
}, 1000); // 1-second delay
});

// Root route to prevent "Cannot GET /"
app.get("/", (req, res) => {
res.send("Welcome to the Snap API! Use /api/snap to see the delay.");
});

// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(API is live on port ${port});
});

GOT IT!!!

but now i have the delay to prevent information from being sent i cant figure out how to get battery percent into it

you cant get device battery from the backend

but maybe nodejs could use a frontend then rescript it before sending the information to snap

thats not how the url block works
as already explained by @ego-lay_atman-bay the URL block returns the data that was sent by the server. it does not run any javascript.

doesnt need to

you just said

you cannot get a devices battery from the backend. you can get it by running javascript on the frontend, however the URL block only returns the data sent from the server. it cannot execute code.

but im using glitch. i can run js

On the backend maybe, but you can't run js on the frontend if you're making an api. Basically, when making an api, no code can run on the frontend, it all has to run on the backend.

so then i dont think getting the device battery percent is possible.

unless yk how to get it in backend