Instructor: Here's our starter project. We have three files, index.html, style.css, and app.js. Index.html will hold our mockup. For the moment, it's basic HTML5 boilerplate that also imports our two other files, style.css and app.js.
The style.css file is already completed, as it will not be the focus of this course. It simply holds the CSS rules that will take care of the layout and the custom look and feel of all of our widgets. The last file, app.js, is totally empty for now. It's where most of the fun will happen, where we will be playing with instantsearch and adding widgets.
Here, we'll keep things simple, no build system. We will load the two libraries via a CDN into our HTML page with two simple script tags. This will expose algoliasearch and instantsearch that we will be using in our app.js file. Last thing to add for the now on this page is the default style for the instantsearch widgets with this link.
Now let's head over to our app.js file. We'll create two variables. The first one, named searchClient, will hold the initialized API client. To do so, we need to pass it our application credentials, the app ID and the API key.
For the API key, make sure to enter a search on the API key, as this will be visible on the front-end source code. You don't want to expose an API key with write access to your indices.
We can now create our second variable that we will simply name search. This will hold the initialized instance of our InstantSearch library where we'll play with widgets. To initialize it, we call the instantsearch function, passing it the indexName we want to target and our previously created searchClient.
The last step to finalize the setup of the lib is to call the start method on our search variable. Let's log our search object to check that everything is working fine.
Let's open the index file in our browser. Nothing happens, which is totally expected for now. If we open the console tab, we can see our search object logged correctly. If something went wrong, like an invalid API key or a missing parameter, you'll see an error message.
Let's remove the app ID to see what that would look like. There you go, a nice error. Let's put it back, and we're all set to start adding widgets.