Curl Connection Refused Error

Trying to automate making cards into Anki from Drafts with the following shell script, using AnkiConnect plugin:

// See online documentation for examples
// https://docs.getdrafts.com/docs/actions/scripting

// define text of bash script
let script = `#!/bin/bash
/usr/bin/curl localhost:8765 -X POST -d "{
\"action\": \"guiAddCards\",
    \"version\": 6,
    \"params\": {
        \"note\": {
            \"deckName\": \"Master\",
            \"modelName\": \"Basic\",
            \"fields\": {
                \"Front\": \"Test\",
                \"Back\": \"Test\"
            },
            \"options\": {
                \"closeAfterAdding\": true
            },
            \"tags\": [
              \"countries\"
            ]
        }
    }
    }"
`;
let runner = ShellScript.create(script);

if (runner.execute(["1", "2"])) {
	alert("STDOUT:\n" + runner.standardOutput);
}
else {
	alert("STDERR:\n" + runner.standardError);
}

Getting this error:

STDERR:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed

0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0
curl: (7) Failed to connect to localhost port 8765: Connection refused

Wondering if any of you have any thoughts?

Can you connect to that local port running a script outside of Drafts?

Also, check that the service is running at all as per the plugin instructions.

Anki must be kept running in the background in order for other applications to be able to use AnkiConnect. You can verify that AnkiConnect is running at any time by accessing localhost:8765 in your browser. If the server is running, you will see the message AnkiConnect displayed in your browser window.

Thanks Syl. You’re right about the “failed to connect” error was because I forgot to start Anki (oops). Still couldn’t get it to work using the above script, I’m attempting another approach…

Thanks again for all of your help!