Solved

Issue adding/updating device via api

  • 21 December 2023
  • 2 replies
  • 33 views

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?

icon

Best answer by felsey 21 December 2023, 23:35

View original

2 replies

I also tried using the v2 endpoint and it also doesn’t work.

 

 const response = await axios.post(
`https://track.customer.io/api/v2/entity`,
{
type: "person",
identifiers: {
id: userId
},
action: "add_device",
device: {
token: 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")}`
}
}
);

 

I’m dumb. It was an issue n our configuration.

Reply