API Reference / API Methods / Initialize the client

It’s recommended to use the Kotlin API client, which is better suited for Android development.

Initialize an index

Before you can initialize an index, you need to set up the client. You need your application ID and API key to do this. You can find both on your Algolia account.

If you are building a native app on mobile, don’t include the search API key directly in your source code. You should instead consider fetching the key from your servers during the app’s startup.

1
2
3
4
5
6
7
8
9
10
11
12
// composer autoload
require __DIR__ . '/vendor/autoload.php';

// if you are not using composer
// require_once 'path/to/algolia/folder/autoload.php';

$client = Algolia\AlgoliaSearch\SearchClient::create(
  'YourApplicationID',
  'YourAdminAPIKey'
);

$index = $client->initIndex('your_index_name');

If you use this API Client with Google AppEngine, the client uses the urlfetch module instead of the request module. Please be aware of urlfetch’s limits. Note that SSL certificates aren’t verified for calls to domains other than algolia.net due to the lack of Server Name Indication (SNI) support in urlfetch. To run unit tests on the AppEngine stub, please define the APPENGINE_RUNTIME environment variable.

You need to replace your_index_name by the name of the index you want to use. If you want to target an existing index you can find the name on the dashboard. If the index doesn’t exist, you can choose any name and Algolia automatically creates it when you perform an addObjects or setSettings operation.

If you see an API key in the preceding snippet, it’s your admin API key. To keep your Algolia data secure, never use your admin API key on your front end or share it with anyone. On your front end, use the search-only API key or any other key that has search-only rights.

Make sure you don’t use any sensitive or personally identifiable information (PII) as your index name, including user’s names, user IDs, or email addresses. Index names appear in network requests, making them publicly available information.

Did you find this page helpful?