Libraries
Last changed
Install recapi's library for your language, make the three calls, and see what it does for you.
In short
- There are eight libraries, all with the same calls and behaviour, and none adds a dependency to your app.
- Three calls are enough: create a client,
trackwhat people do,recommendwhere you show a list. - A library keeps each visitor's history, sends events in the background and falls back to popular items by itself.
- A shop on Shopify, WooCommerce or Magento needs no library: see which platforms.
Install yours
Each name opens the library's page on its registry, with every call and option.
| Library | Install | Needs at least |
|---|---|---|
| JavaScript | npm install @recapi/client |
Node 18 |
| Python | pip install recapi |
Python 3.11 |
| PHP | composer require recapi/client |
PHP 8.2, curl |
| Java, Kotlin | implementation("io.recapi:recapi:0.2.17") |
Java 11, Android 7.0 |
| Go | go get recapi.io/go |
Go 1.24 |
| Swift | .package(url: "https://github.com/Blushedface/recapi-swift.git", from: "0.1.20") |
Swift 6, iOS 15, macOS 12 |
| Ruby | bundle add recapi |
Ruby 3.1 |
| C# | dotnet add package Recapi |
.NET 8 |
- Browsers: Chrome and Edge 93, Firefox 92, Safari 15.4, and later.
- A website without a build step needs no npm: one script tag loads the JavaScript library. On a site with no shop platform it draws the row from your product pages: the website guide.
- A server that can't reach its registry downloads the same library from your recapi address: your dashboard's quick start shows the line.
- Another language calls the API directly.
The three calls
Create one client per page or per process, track what people do, ask for a list. On a server, a visitor who is not logged in has an anon id kept in their session or a cookie (how). The quick start draws the list.
import { createClient } from '@recapi/client';
// consent: false until your cookie banner says yes
const recapi = createClient({
apiKey: 'rk_pub_…',
baseUrl: 'https://api.recapi.io',
consent: false
});
recapi.track(itemId, 'view');
recapi.row('#recommendations', { contextItemId: itemId, heading: 'You may also like' });import recapi
client = recapi.Client("rk_sec_…", "https://api.recapi.io")
client.track("item_4471", "view", user_id="user-881", anon_id=anon_id)
result = client.recommend(user_id="user-881", anon_id=anon_id, placement="homepage", count=4)if (session_status() === PHP_SESSION_NONE) session_start();
$recapi = new Recapi\Client('rk_sec_…', 'https://api.recapi.io', store: new Recapi\SessionStore());
$recapi->track('item_4471', 'view', userId: 'user-881', anonId: $anonId);
$result = $recapi->recommend(userId: 'user-881', anonId: $anonId, placement: 'homepage', count: 4);Client recapi = new Client("rk_sec_…", "https://api.recapi.io");
recapi.track(new Event("sku-42", "view").userId("cust-881").anonId(anonId));
Recommendations recs = recapi.recommend(new RecommendRequest().userId("cust-881").anonId(anonId).count(4));client, err := recapi.New("rk_sec_…", "https://api.recapi.io", recapi.Options{})
client.Track(ctx, recapi.Event{ItemID: "sku-42", Level: recapi.View, UserID: "cust-881", AnonID: anonID})
recs, err := client.Recommend(ctx, recapi.Request{UserID: "cust-881", AnonID: anonID, Count: 4})let recapi = try RecapiClient(apiKey: "rk_sec_...", baseURL: "https://api.recapi.io")
try recapi.track(itemID: "sku-42", level: Level.view, userID: "user-7", anonID: anonID)
let list = try await recapi.recommend(RecommendRequest(userID: "user-7", anonID: anonID, count: 4))RECAPI = Recapi::Client.new("rk_sec_…", "https://api.recapi.io")
RECAPI.track(product.id, :view, user_id: current_user&.id, anon_id: anon_id)
recs = RECAPI.recommend(user_id: current_user&.id, anon_id: anon_id, count: 4)await using var client = new RecapiClient("rk_sec_…", "https://api.recapi.io");
await client.TrackAsync(new RecapiEvent { ItemId = "sku-42", Level = Levels.View, UserId = "cust-881", AnonId = anonId });
var recs = await client.RecommendAsync(new RecommendRequest { UserId = "cust-881", AnonId = anonId, Count = 4 });Which key, and PHP's session
A browser or an app takes a public key (rk_pub_…), a server a secret one (rk_sec_…): see
keys. On public PHP pages behind a cache, use the script tag: a PHP session stops the cache.
Batching
track returns at once and the library sends events together, so tracking never slows a page. Each event carries its
own id: one sent twice counts once.
| Library | When events leave |
|---|---|
| JavaScript | every 2 seconds, up to 100 a request, and when the page is hidden |
| PHP | at the end of the request, after the page has been sent (PHP-FPM, LiteSpeed) |
| The others | every 2 seconds, up to 1,000 a request, and when the client closes |
A short job, a serverless function or an app going to the background sends what is queued first:
await recapi.flush();client.flush()$recapi->flush();recapi.flush();client.Flush()try await recapi.flush()RECAPI.flushawait recapi.FlushAsync();History
The library keeps what each visitor did and sends it with every recommend, so a list follows the last click
(what it holds).
- In the browser, it lives on the visitor's device once they consent.
- On a server, it lives in memory for the 10,000 most recent users of each process. Several servers share it
through the
storeyou pass, such as Redis. - In PHP, pass a store: without one, the history lasts one request.
- In an app, pass the file store (Java, Swift) so it survives a restart.
When a visitor logs in, say who they are, and what they did before still counts:
recapi.identify("u_812"); // at login
recapi.reset(); // at logoutclient.identify(session["recapi_anon"], "u_812") # at login$recapi->identify($_SESSION['recapi_anon'], 'u_812'); // at loginrecapi.identify(visitorId, "u_812"); // at loginclient.Identify(ctx, visitorID, "u_812") // at logintry recapi.identify(anonID: visitorID, userID: "u_812") // at loginRECAPI.identify(session[:recapi_anon], "u_812") # at loginawait recapi.IdentifyAsync(visitorId, "u_812"); // at loginConsent
Only the JavaScript library puts anything in a browser, and only after you pass on a yes. Connect it to your cookie banner (what happens without consent):
const recapi = createClient({ apiKey, baseUrl, consent: cmp.hasConsent('personalisation') });
cmp.onChange((granted) => recapi.setConsent(granted));
The server libraries set no cookie. Two stores you may choose do keep something with the visitor: PHP's SessionStore
(a session cookie) and an app's file store.
To erase a person, call your server's library:
// Needs a secret key: call it from your server (other tabs).client.delete_user("u_812")$recapi->deleteUser('u_812');recapi.deleteUser("u_812");err := client.DeleteUser(ctx, "u_812")try await recapi.deleteUser("u_812")RECAPI.delete_user("u_812")await recapi.DeleteUserAsync("u_812");Fallbacks
recommend always gives your page something to show, or an empty list to hide. It waits 1.5 seconds at most.
| What happened | What you get |
|---|---|
| recapi is slow, down, or over a limit | popular items, marked as a fallback |
| popular items fail too, or the key is refused | an empty list: hide the row |
| a product id recapi can't accept | popular items, and one line in your log |
| a mistake in the call itself | an error, on the first run (errors) |
Retries
Events are retried, lists are not: a late list is worthless.
| What failed | What the library does |
|---|---|
| Events: network, timeout, rate limit, server error | three more tries, from half a second apart or as Retry-After asks, then keeps them for later |
| Events: refused (wrong key, monthly limit, bad request) | drops them and says so in your log |
| Events at the end of a PHP page | one try of 2 seconds, so an outage holds your server 2 seconds at most |
| Items sent and people erased | retried like events, then an error |
Up to 10,000 events wait in a server's memory during an outage, 1,000 in a browser; past that the oldest go first.
Names in each language
What the same thing is called where you work.
| Language | Fallback mark | Error type | Where problems are logged |
|---|---|---|---|
| JavaScript | fallback |
RecapiError |
onError, else the console |
| Python | fallback |
RecapiError |
the recapi logger |
| PHP | fallback |
RecapiError |
logger, else error_log() |
| Java | isFallback() |
RecapiException |
the io.recapi logger |
| Go | Fallback |
recapi.Error |
Logger, else slog |
| Swift | fallback |
RecapiError |
standard error |
| Ruby | fallback? |
Recapi::Error |
logger, else standard error |
| C# | Fallback |
RecapiException |
Log, else standard error |
Something doesn't work? Troubleshooting starts from what you see.