Pushing data to an API trough Keyboard Maestro

Hi, I was trying to create a macro on Keyboard Maestro to help me push some data to YNAB API (which is awesome, btw), but could not find a way to place an API call with my variables.

That’s kinda odd, since I’m able to do this very easily on iOS Shortcuts…

Can you guys point me to a very simple way to help me automate this on my Mac?

The recommendation from the KM developer is to use curl.

I’ve managed to place a few commands using curl, but am really miserably failing when trying to pass KBM variables onto a curl request.

I’ve tried this:

curl -v -XPOST https://api.airtable.com/v0/XXXXX/Faturas \
-H "Authorization: Bearer XXXXXX" \
-H "Content-Type: application/json" \
--data '{
"fields": {
"Emissão": "${KMVAR_Emissao}",
"Valor": "${KMVAR_Valor}",
"Vencimento": "${KMVAR_Vencimento}",
"Pagamento": "Aberto"
}
}'

But only get this response:

{"error":{"type":"INVALID_VALUE_FOR_COLUMN","message":"Cannot parse date value \"${KMVAR_Emissao}\" for field Emissão"}}

I guess KBM is not replacing the variables on the curl command. How can I fix this?

Emissao is a yyy-MM-dd formatted date, btw.

I’m just about to turn in as it is rather late here in the UK, so I don’t have time to check right now.

I don’t see anything obviously wrong in terms of quotes, KM variable formats or the like (I haven’t checked the curl parameters). However, try echo-ing out the command you are sending and tracking what substitutions are actually being made, if any. Check that your date variable is actually set by simply outputting each it, or in fact each parameter, with an echo before the curl line. Perhaps even check with an action output in KM itself to rule out setting vs. substitution issues.

Might be irrelevant but in ‘Emissão’ there is an interesting character. It might be being squashed.

Did you figure this one out? I am also looking for a way to use Keyboard Maestro to push data to Airtable.

Can you please share the steps.

It took me quite a while to grasp, but the answer was here as kindly put by @peternlewis himself, in Keyboard Maestro Forum.

Here’s the corresponding Keyboard Maestro Wiki entry. Look under the emacs example.

For me, I needed to replace every "$KMVAR_Variable" to "'"$KMVAR_Variable"'"

That’s because the Shell Script would not work if the statement is already between ', such as the data in the default curl script.

In the end, my curl command would read like this

curl -v -X POST https://api.airtable.com/v0/XXXXXXX/Table%201 \
  -H "Authorization: Bearer XXXXXX" \
  -H "Content-Type: application/json" \
  --data '{
  "records": [
    {
      "fields": {"Name": "'"$KMVAR_Var1"'", "Notes": "'"$KMVAR_Var2"'"}
    }
    ]
}'

If you happen to be a complete novice on curl as I still am, I’d recommend copying the codes from api.airtable.com and trying out yourself. It may look a bit daunting at first, but it most surely is not!

Thank you. Made it work. For anyone looking for a solution in the future.

  1. Open your AirTable
  2. Top Right Corner - Help - API Documentation
  3. On the Left find “Create records”

You will see a script for your AirTable that needs to be pasted into Keyboard Maestro Shell Window.

Whichever variable you create, in the script it has to be as '"$KMVAR_YourVariable"' then put it inside the " " of the AirTable curl script.

1 Like

Please note that this approach is good for string-type fields but will not work for number fields. For them, try '"123"' instead of "'"123"'"