Installation
- JavaScript
- PHP
To get started, you first need to install algoliasearch
(or any other available API client package). You can find the full list of available packages here.
All of our clients comes with type definition, and are available for both browser
and node
environments.
yarn add @experimental-api-clients-automation/algoliasearch
# or
npm install @experimental-api-clients-automation/algoliasearch
Or use a specific package:
yarn add @experimental-api-clients-automation/client-search
# or
npm install @experimental-api-clients-automation/client-search
Without a package manager
Add the following JavaScript snippet to the <head>
of your website:
<script src="https://cdn.jsdelivr.net/npm/@experimental-api-clients-automation/algoliasearch/dist/algoliasearch.umd.browser.js"></script>
Using the client
Then you need to import the correct package:
import { algoliasearch } from '@experimental-api-clients-automation/algoliasearch';
const client = algoliasearch('<YOUR_APP_ID>', '<YOUR_API_KEY>');
// And access analytics or personalization client
const analyticsClient = client.initAnalytics('<YOUR_APP_ID>', '<YOUR_API_KEY>');
const personalizationClient = client.initPersonalization(
'<YOUR_APP_ID>',
'<YOUR_API_KEY>',
'eu'
);
const searchRes = await client.search({
indexName: '<YOUR_INDEX>',
searchParams: { query: searchQuery },
});
const analyticsRes = await analyticsClient.getTopFilterForAttribute({
attribute: 'myAttribute1,myAttribute2',
index: '<YOUR_INDEX>',
});
console.log('[Results]', searchRes, analyticsRes);
import { searchClient } from '@experimental-api-clients-automation/client-search';
const client = searchClient('<YOUR_APP_ID>', '<YOUR_API_KEY>');
It is possible to customize the client by passing optional parameters:
const client = searchClient('<YOUR_APP_ID>', '<YOUR_API_KEY>', {
requester: customRequester(),
hosts: [
{
url: 'my.custom.host.net',
accept: 'read',
protocol: 'https',
},
],
});
Once the client is setup, you can play with the Algolia API!
const res = await client.search({
indexName: '<YOUR_INDEX>',
searchParams: { query: 'words to query' },
});
console.log('[Results]', res);
Or with the personalization
client
const res = await personalizationClient.getUserTokenProfile({
userToken: 'token',
});
console.log('[Results]', res);
First, install Algolia PHP API Client via the composer package manager:
composer require algolia/algoliasearch-client-php
Using the client
Then, create objects on your index:
$client = Algolia\AlgoliaSearch\Api\SearchClient::create(
'<YOUR_APP_ID>',
'<YOUR_API_KEY>'
);
$client->saveObject('<YOUR_INDEX>', ['objectID' => 1, 'name' => 'Foo']);
Finally, you may begin searching an object using the search
method:
$objects = $client->search('<YOUR_INDEX>', ['query' => 'Foo']);
Another example with the personalization client:
$client = Algolia\AlgoliaSearch\Api\PersonalizationClient::create(
'<YOUR_APP_ID>',
'<YOUR_API_KEY>'
);
$res = $client->getUserTokenProfile('<YOUR_TOKEN>');