Instantiate the client/index
Init Index
To begin, you will need to initialize the client. In order to do this you will need your Application ID and API Key. You can find both on your Algolia account.
If you are building a native app on mobile, be sure to not include the search API key directly in the 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, it will use urlfetch
instead of using the request
module. Please be aware of the limits of urlfetch
, and note that SSL certificates aren’t verified for calls to domains other than algolia.net due to the lack of SNI support in urlfetch
. To run unit tests on the AppEngine stub, please define an APPENGINE_RUNTIME
enviroment 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 from the dashboard.
If the index does not exist you can choose any name and it will be created when you perform an add objects
or a set settings operation.
If you see an API key in the following snippet, it is your Admin API Key. To maintain security, never use your Admin API Key on your front end or share it with anyone. In 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 customer names, user IDs, or email addresses. Index names appear in network requests and should be considered publicly available.
Client options
In most situations, there is no need to tune the options. We provide this list for transparency.
timeout
(Number
): the timeout for requests to our servers, in milliseconds- in Node.js, this is an inactivity timeout (default:
15000
) - in the browser, this is a global timeout (default:
2000
incremental)
- in Node.js, this is an inactivity timeout (default:
protocol
(String
): the protocol that should be used when communicating with Algolia (default:https:
)- this applies to both the browser and Node.js
- possible values:
http:
,https:
hosts.read
([String]
): the array of read hosts that is used to call Algolia servers, automatically computedhosts.write
([String]
): the array of write hosts that is used to call Algolia servers, automatically computed
Node.js-only parameters:
httpAgent
:http.Agent
instance to use when communicating with Algolia servers
To pass an option, use:
1
2
3
const client = algoliasearch(applicationId, apiKey, {
timeout: 4000,
});
Proxy support
Node.js only
If you are behind a proxy, use the HTTP_PROXY
or HTTPS_PROXY
environment variables before starting your Node.js program:
1
HTTP_PROXY=http://someproxy.com:9320 node main.js