I am trying to add or update devices to users via Node and a put request. Neither seem to work.
The response is an an empty object and I’m not seeing a 400, so I assume it think it’s working, but when looking at the person in customer there is no device.
Here is my code:
import { NextApiRequest, NextApiResponse } from "next";
import { TrackClient, RegionUS } from "customerio-node";
import Environment from "shared/utils/environment/environment";
import axios from "axios";
const analytics = new TrackClient(
Environment.customerioSiteId,
Environment.customerioApiKey,
{ region: RegionUS }
);
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const {
userId,
deviceId,
lastUsed,
deviceOS,
deviceModel,
appVersion,
pushEnabled
} = req.body;
if (!userId) {
res.status(400).json({ error: "userId is required" });
}
if (!deviceId) {
res.status(400).json({ error: "deviceId is required" });
}
console.log("about to add Device");
console.log("userId", userId);
console.log("deviceId", deviceId);
// const response = await analytics.addDevice(
// userId,
// deviceId,
// "ios"
// // {
// // last_used: lastUsed,
// // attributes: {
// // device_os: deviceOS,
// // device_model: deviceModel,
// // app_version: appVersion,
// // device_locale: deviceLocale,
// // push_enabled: pushEnabled
// // }
// // }
// );
const response = await axios.put(
`https://track.customer.io/api/v1/customers/${userId}/devices`,
{
device: {
id: deviceId,
last_used: lastUsed,
platform: "ios",
attributes: {
device_os: deviceOS,
device_model: deviceModel,
app_version: appVersion,
push_enabled: pushEnabled
}
}
},
{
headers: {
"Content-Type": "application/json",
Authorization: `Basic ${Buffer.from(
`${Environment.customerioSiteId}:${Environment.customerioApiKey}`
).toString("base64")}`
}
}
);
console.log("response", response.data);
res.status(200).json({ status: "ok" });
}
What am I missing?