Skip to content

Instantly share code, notes, and snippets.

@tfrench15
Created April 14, 2021 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfrench15/0b45a79c83c5a21c23e6f1301ef5ed98 to your computer and use it in GitHub Desktop.
Save tfrench15/0b45a79c83c5a21c23e6f1301ef5ed98 to your computer and use it in GitHub Desktop.
// This function calls Segment's Profile API to fetch a user's Traits. Similarly, we can fetch
// a user's Event history, Metadata, Account Hierarchies, and more.
const axios = require('axios');
exports.handler = function(context, event, callback) {
// Initialize an axios client with the base URL of Segment's Profile API and your Segment API token
// for Basic Authentication.
const profile = axios.create({
baseURL: `https://profiles.segment.com/v1/spaces/${context.spaceId}/collections/users/profiles`,
auth: {
username: context.token,
password: '',
}
});
// Fetch a user's traits using email as the identifier. To fetch a user's event history, query the `/events`
// endpoint.
profile
.get(`/email:${event.email}/traits`)
.then((response) => {
return callback(null, response.data.traits);
})
.catch((error) => {
console.log(error);
return callback(error);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment