You wont be able to record a Zest script via the API, but you hopefully wont need to.
I always recommend making full use of the UI, even if you want to end up just using the API.
Record you Zest script via the GUI and check that it works. This FAQ may help with authentication in general:
https://github.com/zaproxy/zaproxy/wiki/FAQformauthOnce you've got is working in the GUI then save the Context.
Then with a new session (still in the GUI) import the Context via the API and check authentication still works.
I strongly recommend testing the API with the GUI - its much easier to see what going on.
Once the API is working fine (testing via the GUI) you should be able to run ZAP in daemon mode and run the same API calls and it should still work in the same way.
If you need to run the script on another machine then you'll also need to copy the Zest script over and import it via the API as well.
Cheers,
Simon