Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools MailChimp API MailChimp API Adding New Members to Your List

medev21
medev21
18,291 Points

How do I use mailchimp api key on a html button?

I'm trying a to do a one page static html file, with a form and button, users should be able to click the button and submit their email. Where does the api come in? do i need to create js file? if so, can you give a quick example. I've been trying to figure this out for not luck. Please help!!!

1 Answer

You would construct a HTML form with all the necessary fields, then use some JavaScript or jQuery or similar to turn the form values into JSON, and send a POST request with that data, along with your API key, to the API endpoint mentioned in the video.

The real issue you will have is that the API key would be visible in the JavaScript, so technically anyone could get access to it and use it with your MailChimp account.

Normally API keys are stored and accessed only on the server for this very reason.

medev21
medev21
18,291 Points

Hey Iain , thanks for your comment. Quick questions for you, can you use php/jquery without a database? I would like to use php that way the code will not be visible in the source view, that's what i'm hoping. How are the API keys are stored in the server? I'm not sure what you mean by server, like hosting service namecheap? I'm novice when it comes to this stuff, I always stored the api's in heroku, but I want to find other viable ways?

Hi medev21, yes, you can definitely use PHP and jQuery without a database. Using a database just means you have a way to store data that can be accessed later. If you only want to use the data to make an API request and that's it, then there's no need to store it.

By server, I mean the web server, and server-side programming/scripting languages. The other side is the client-side, which is essentially the browser environment that the user is in, with JavaScript being the main scripting language there.

If you're using PHP to make the API calls, then you would store the API key in a variable in PHP (normally in a separate file that you also wouldn't put in a code repository like Github, also for security reasons).

Heroku was built to provide an environment to work with many types of web development technologies, so there's no issues with using that, and in fact, it's one of the most popular platforms for doing so.