This topic was automatically generated from Slack. You can find the original thread here.
Hi folks, curious if anyone knows of a free API that simply lets one set or get a value via API request.
I have a simple little workflow that fetches the value of daily sales from the POS API, then sends that value to a counter API (https://countapi.xyz/), which is then in turn fetched by some analog desktop displays (https://www.smiirl.com/), and folks can see sales as they come in (and even hear them, since the display makes a satisfying clickity-click sound when it updates). The problem I’m running into is the counter API seems to have gone down, and hasn’t gotten back up again.
I looked around for replacements, and found these folks: Counter API - API Ninjas, but unfortunately they require an API key to be included in the header even to get the value, and unfortunately the Smiirl counters don’t allow for setting headers when they go fetch a value.
Currently I have modified my workflow to simply push the value to each counter, but this approach won’t scale very well as more Smiirls are added…I much prefer the workflow to simply update a value once and then the counters can all fetch that value from one source.
You can build this yourself with a Pipedream workflow! I’d like to build a workflow triggered on an HTTP request that increments a counter. I want to ping it with an HTTP POST request to increment the counter, and use an HTTP GET request to retrieve the current value of the counter. Can you show me how to build this Pipedream workflow?
also, I need to set a workflow concurrency of 1 worker to make sure multiple requests don’t update the counter only once. Tell me more about workflow concurrency and how I can configure this in my workflow.
Sure! You can create a Pipedream workflow with an HTTP trigger to handle both POST and GET requests. You’ll also need to use a key-value store to maintain the counter value. I recommend using Pipedream’s built-in key-value store. Here’s an example workflow: 1. Create a new workflow and add an HTTP / Webhook Requests trigger. 2. Add a new Code step and use the following code: import { axios } from "@pipedream/platform"; const counterKey = "my-counter"; async function getCounter() { const response = await axios(this, { method: "GET", url: `https://api.pipedream.com/v1/key_value_stores/${counterKey}`, headers: { Authorization: `Bearer ${process.env.PIPEDREAM_API_KEY}`, }, }); return response.data.value; } async function incrementCounter() { const currentValue = await getCounter(); const newValue = currentValue + 1; await axios(this, { method: "PUT", url: `https://api.pipedream.com/v1/key_value_stores/${counterKey}`, headers: { Authorization: `Bearer ${process.env.PIPEDREAM_API_KEY}`, "Content-Type": "application/json", }, data: { value: newValue, }, }); return newValue; } if (steps.trigger.event.method === "POST") { const newValue = await incrementCounter(); return { counter: newValue }; } else if (steps.trigger.event.method === "GET") { const currentValue = await getCounter(); return { counter: currentValue }; } else { return { error: "Unsupported method" }; } Make sure to replace my-counter with your desired counter key name. 3. Deploy the workflow. Now, when you send a POST request to the trigger URL, it will increment the counter, and when you send a GET request, it will return the current value of the counter. Please note that you’ll need to set the PIPEDREAM_API_KEY environment variable in your workflow settings to your Pipedream API key. You can find your API key in your Pipedream account settings.
Concurrency controls in Pipedream define how many events can be executed in parallel. By setting the concurrency to 1 worker, you enforce serialized, in-order execution, which ensures that each event is processed only once the execution for the previous event is complete. To configure concurrency in your workflow, follow these steps: 1. Open your workflow in the Pipedream UI. 2. Click on the “Settings” tab at the top of the page. 3. Scroll down to the “Concurrency” section. 4. Set the number of workers to 1. By setting the workflow concurrency to 1 worker, you’ll ensure that multiple requests won’t update the counter simultaneously, avoiding any potential issues.
Apologies for the confusion earlier. Here’s a revised workflow using Pipedream’s built-in key-value store: 1. Create a new workflow and add an HTTP / Webhook Requests trigger. 2. Add a new Code step and use the following code: const counterKey = "my-counter"; async function getCounter() { const response = await $store.get(counterKey); return response || 0; } async function incrementCounter() { const currentValue = await getCounter(); const newValue = currentValue + 1; await $store.set(counterKey, newValue); return newValue; } if (steps.trigger.event.method === "POST") { const newValue = await incrementCounter(); return { counter: newValue }; } else if (steps.trigger.event.method === "GET") { const currentValue = await getCounter(); return { counter: currentValue }; } else { return { error: "Unsupported method" }; } Make sure to replace my-counter with your desired counter key name. 3. Deploy the workflow. Now, when you send a POST request to the trigger URL, it will increment the counter, and when you send a GET request, it will return the current value of the counter.
I wish these Slack responses trained the model but not yet! Writing up a big blog post on how Pi works and it’ll detail your process for improving these responses.
The core issue here is that I want this to run as frequently as possible, with the constraint of how many events I can run per day. Currently it runs every 3 minutes, and only between the hours of 9am and 11pm. The Smiirl counters are much dumber than pipedream, so they cannot be configured to only request updated values when the stores are open, they would run 24/7, which would require me to set their frequency lower so that I don’t max out my credits. The benefit of using a 3rd party API is that the counters can hit it as often as they want without bothering pipedream and using up credits…those are only used up when the workflow fetches the value and sets the 3rd party API value.
Thank you as always for your rapid and enthusiastic support! hopefully if i can get this all ironed out at some point i’ll be able to upgrade to a real plan and start selling these things
I looked at Smiirl, having liked flipped displays for some time. I wish they had a more custom unit, such that the left most flip could flip to different icons. That way periodically I could have the unit show Instagram subscriber counts, then another time store sales and then another time YouTube subscriber count, etc. I’d be with some hardware hacking and cleaver code work I could make it happen.
I was thinking the same thing Tim…having it switch through different stores, then the total, etc. There’s some larger (more expensive too) displays with more rows, colors, icons etc that I looked at, but smiirl seemed to hit the sweet spot in terms of cost and aesthetics for now. If you figure out a good way to rotate the values like that, give a shout, I’d like to try it out as well!