It's a back end only API.
You're not supposed to ship this API key with your JavaScript application. It's intended for back end use only, where you can keep the key private.
Probably the issue described in no more detail than "it does not work", is because Mailchimp will block the request if you try to use the key from a browser. Their documentation describes in detail.
Because of the potential security risks associated with exposing account API keys, Mailchimp does not support client-side implementation of our API using CORS requests or including API keys in mobile apps.
If you still want to use the API for this, you'll have to set up your own back end service which receives the data from the front end of your site and forwards it to Mailchimp.
For example, if your website uses PHP, you could preserve your form's JS code, but point it to your own custom endpoint / PHP file instead.
Mailchimp has a PHP client library you could use to make crafting the HTTP request more robust and less verbose. But you could also do it "manually" if you also don't want to install a PHP library.
// form-submission.php
function read_key() {
// Could also come from other source, like environment variables.
// As long as it's in a safe place and can't be leaked.
return file_get_contents(SECRET_KEY_LOCATION);
}
$apiKey = read_key();
require_once('/path/to/MailchimpMarketing/vendor/autoload.php');
$mailchimp = new MailchimpMarketing\ApiClient();
$mailchimp->setConfig([
'apiKey' => $api_key,
'server' => 'YOUR_SERVER_PREFIX'
]);
$response = $mailchimp->lists->addListMember( /* ... form data */);
print_r($response);
Depending on your use case you may need to use one of the many other API endpoints.