[Alpha]{class="badge informative"}
Install the Server SDK
The Server SDK is available as the @adobe/alloy-node package on npm. Install it in your server project with npm, or with the package manager that your runtime uses:
npm install @adobe/alloy-node
The package is published as ECMAScript modules and runs in any server-side JavaScript runtime that meets the requirements. Load it with import:
import { createInstance } from "@adobe/alloy-node";
process.env and the node:http module, with your runtime’s equivalents. See Runtime support.Create an instance create-instance
The createInstance function returns an instance of the SDK that includes every available optional component (Consent and Personalization). Most implementations use this function.
import { createInstance } from "@adobe/alloy-node";
const alloy = createInstance({ name: "alloy" });
The createInstance function accepts an optional object with the following properties:
name[alloy]. Defaults to "alloy". Set a different name if you run more than one instance in the same process and want to tell their logs apart.monitorsonInstanceCreated, onInstanceConfigured, and onBeforeCommand, that the SDK calls as it runs. These hooks work the same way as Web SDK monitoring hooks.platformServicesThe returned instance exposes methods, such as configure, forRequest, and sendEvent. Unlike the Web SDK, you call methods directly on the instance, for example alloy.sendEvent({ ... }), instead of passing the command name as a string.
Create a custom instance custom-instance
If you don’t need every optional component, use createCustomInstance and pass only the components that you want. The components export contains each optional component:
components.consentsetConsent method and the defaultConsent configuration option.components.personalizationpersonalization option of sendEvent.import { createCustomInstance, components } from "@adobe/alloy-node";
// Includes consent, but not personalization
const alloy = createCustomInstance({
components: [components.consent]
});
The Context, data collection, identity, and library information components are always included. When you leave out an optional component:
- Methods that the component provides still exist on the instance, but they reject when called. For example,
setConsentrejects with an error whose message includesThe setConsent command does not exist. - Configuration options that the component provides are rejected by
configure. For example,defaultConsentcausesconfigureto reject with anUnknown fielderror. - Options that the component reads in other methods are ignored. For example,
sendEventdoesn’t request personalization even if you set thepersonalizationoption.
createCustomInstance accepts the same name, monitors, and platformServices properties as createInstance.
Send a test event test-event
After you set up authentication, you can confirm that your installation works by configuring the instance and sending one event. Store your credentials in environment variables instead of in source code.
import { createInstance } from "@adobe/alloy-node";
const alloy = createInstance();
await alloy.configure({
datastreamId: "ebebf826-a01f-4458-8cec-ef61de241c93",
orgId: "ADB3LETTERSANDNUMBERS@AdobeOrg",
edgeCredentials: {
clientId: process.env.ADOBE_CLIENT_ID,
clientSecret: process.env.ADOBE_CLIENT_SECRET,
scopes: process.env.ADOBE_SCOPES.split(",")
}
});
const result = await alloy.sendEvent({
xdm: {
eventType: "web.webpagedetails.pageViews",
identityMap: {
Email: [{ id: "test-visitor@example.com", primary: true }]
}
}
});
console.log("Event sent", result);
If the event succeeds, the promise resolves. If it fails, the promise rejects with an error that describes the problem, such as invalid credentials or a missing primary identity. Set debugEnabled to true to log each step to the console.
forRequest handle for each visitor’s request instead. See Instances and requests.