Skip to main content
Solved

Issue adding/updating device via api

  • December 21, 2023
  • 2 replies
  • 113 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?

Best answer by felsey

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

View original
Did this topic help you find an answer to your question?

2 replies

  • Author
  • Novice
  • 2 replies
  • December 21, 2023

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")}`
      }
    }
  );

 


  • Author
  • Novice
  • 2 replies
  • Answer
  • December 21, 2023

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


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings