Is there a way to make macros using code instead of the GUI drag and drop?

One variant is to use a Copy as JSON macro in place of the built-in Keyboard Maestro Edit > Copy As > Copy As XML

In lieu of working with plist strings like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>ActionUID</key>
        <integer>11082764</integer>
        <key>IsDisclosed</key>
        <false/>
        <key>JustDisplay</key>
        <false/>
        <key>MacroActionType</key>
        <string>SetClipboardToText</string>
        <key>Text</key>
        <string></string>
    </dict>
</array>
</plist>

you can then script with corresponding JS Arrays and Objects like:

[
    {
      "IsDisclosed": false,
      "JustDisplay": false,
      "Text": "",
      "MacroActionType": "SetClipboardToText",
      "ActionUID": 11082764
    }
]

and convert the script-generated custom JS objects to plist XML strings either at run-time, or in a final build stage, for use in this pattern:

Application("Keyboard Maestro Engine").doScript(plist);

An old thread here, perhaps ready now for some updating and simplification: