Beeminder Charge API with Shortcuts

I have been testing the beeminder charge API. With the help of my good friend chat GPT 4 I have been able to attach it to iOS shortcuts using the scriptable app. Basically I set it up where when I open a distracting app it uses the Beeminder Charge API to charge me a dollar. I am also working on setting it up for when I use the credit card. That shortcut will activate when I get an automated text from credit card company (iOS 17 beta feature that allows it to work without interaction).


const apiKey = 'APIKEY';  // Replace with your key
const username = "Username"; // Replace with your username

async function createCharge(amount, note) {
    const endpoint = `https://www.beeminder.com/api/v1/charges`;
    
    const request = new Request(endpoint);
    request.method = 'POST';
    request.headers = {
        'Content-Type': 'application/json'
    };
    request.body = JSON.stringify({
        user_id: username,
        amount: amount,
        note: note,
        auth_token: apiKey,
        dryrun: false
    });
    
    const response = await request.loadJSON();

    if(response.id) {
        console.log("Charge created successfully:", response);
        // Optionally, display a notification
        const notification = new Notification();
        notification.title = "Charge Created";
        notification.body = `Successfully created a charge of $${amount}. Note: ${note}`;
        notification.schedule();
    } else {
        console.error("Error creating charge:", response);
    }
}

// To create a $1 charge:
createCharge(1, "Non allowed app opened $1 fine.");

3 Likes