Skip to main content

Ship pre-configured devices

Now that we know how to sign requests, we can create of first order for pre-configured device(s).

The createuserorder webservice allows partners to ship preconfigured devices to their users by leveraging Withings Logistics services. This API serves the following purposes:

  • Create a Withings user account
  • Place a dropshipment order for device(s) specified in the request
  • Preconfigure the cellular device(s) for the user

Run In Postman

Notes

  • Invalid addresses in the order will result in the entire request being rejected.
  • The authorization code returned in this API is valid for 10 minutes.

Once you've created the order and user, you can proceed to the next step to learn how to exchange the code for access and refresh tokens.

Parameters

Refer to the API reference or

Toggle me!
ParameterRequiredTypeDescription
actionYesstringThe service action name. Must be set to createuserorder.
client_idYesstringThe ID of your client.
nonceYesstringA random token used to prevent replay attacks. Refer to the Signature v2 - Getnonce.
signatureYesstringA hash of the parameters. See Signature hash protocol for details.
mailingprefYesbooleanIndicates whether the customer has accepted Withings commercial contacts.
birthdateYesintegerThe Unix timestamp of the user's birthdate.
measuresYesjson_stringUser measures (refer to Measures model).
genderYesintegerGender of the user: 0 for male, 1 for female.
preflangYesstringUser's language preference (e.g., en_EN, fr_FR).
unit_prefYesjsonUser's unit preferences (refer to Unit preferences model).
emailYesemailThe user's email address.
timezoneYestimezoneUser's timezone (e.g., Europe/Paris, America/New_York). A complete list can be found here.
shortnameYesstringA three-character representation of the end user (e.g., ROB for Robert). Must match regex: /^[a-zA-Z0-9]{3}$/.
external_idYesstringA unique identifier used by the partner to identify the end user.
orderYesjsonOrder details (refer to Order model).
testmodeNointegerEnable test mode to simulate order creation without shipping physical devices.

Order Model

The order parameter in the Dropshipment service is a list of JSON objects representing product purchase orders. Each order must contain the following information:

Order

NameTypeRequiredDescription
customer_ref_idstringYesA unique identifier for the order provided by the partner. It is used to track the order.
addressjsonYesDelivery information for the order. Refer to the Address section below for details.
productsjsonYesA list of products in the order. Refer to the Products section below for details.
force_addressbooleanNoIf true, the address will not be validated by carriers. Default is false.

Address

NameTypeRequiredDescription
namestringYesFull name of the recipient.
company_namestringNoName of the company (if applicable).
emailstringYesEmail address of the recipient.
telephonestringNoPhone number in E.164 format.
address1stringYesPrimary address (e.g., street address with number).
address2stringNoAdditional address details (e.g., apartment or suite number).
citystringYesCity of the recipient.
zipstringYesZIP or postal code.
statestringNoState of the recipient (ISO2 format).
countrystringYesCountry of the recipient (ISO2 format).

Products

NameTypeRequiredDescription
quantityintegerYesQuantity of the product in the order.
eanstringNoProduct's international article number. Required if partner_ref is not used.
partner_refstringNoThe reference of the Withings device kit shared during the project. Required if ean is not used.

Note: Either ean or partner_ref must be provided for each product.

Example

Below is an example of the order parameter in JSON format:

[
{
"customer_ref_id": "UNIQUE_ORDER_123",
"address": {
"name": "John Doe",
"email": "john.doe@example.com",
"telephone": "+1234567890",
"address1": "123 Main Street",
"address2": "Apt 4B",
"city": "New York",
"zip": "10001",
"state": "NY",
"country": "US"
},
"products": [
{
"quantity": 1,
"partner_ref": "WITHINGS_DEVICE_KIT_001"
},
{
"quantity": 2,
"ean": "1234567890123"
}
]
},
{
"customer_ref_id": "UNIQUE_ORDER_456",
"address": {
"name": "Jane Smith",
"email": "jane.smith@example.com",
"address1": "456 Elm Street",
"city": "San Francisco",
"zip": "94107",
"country": "US"
},
"products": [
{
"quantity": 3,
"partner_ref": "WITHINGS_DEVICE_KIT_002"
}
]
}
]

Snippet

const axios = require('axios');
const { sign, getNonce } = require('../signature');

// Constants
const API_ENDPOINT = 'https://wbsapi.withings.net/';
const OAUTH2_GRANT_TYPE_AUTHORIZATION_CODE = 'authorization_code';
const REDIRECT_URI = 'YOUR_REDIRECT_URI';
const CLIENT_ID = 'YOURCLIENTID';
const CLIENT_SECRET = 'YOURCLIENTSECRET';

const MEASURE_TYPE_HEIGHT = 4;
const MEASURE_TYPE_WEIGHT = 1;
const UNIT_HEIGHT_M  = 6;
const UNIT_HEIGHT_IN = 7;
const UNIT_WEIGHT_KG = 1;
const UNIT_WEIGHT_LB = 2;
const UNIT_WEIGHT_STLB = 14;
const UNIT_DISTANCE_KM = 6;
const UNIT_DISTANCE_YD = 8;
const UNIT_TEMPERATURE_C = 11;
const UNIT_TEMPERATURE_F = 13;

const TESTMODE_SHIPPED = 1;
const TESTMODE_TRASHED= 2;
const TESTMODE_FAILED = 3;
const TESTMODE_BACKHOLD = 4;


async function createUserOrder(nonce, order, user, testmode) {
  const params = {
    action: 'createuserorder',
    client_id: CLIENT_ID,
    nonce,
    order: JSON.stringify([order]), // order should always be passed a JSON array of Order objects
    email: user.email,
    shortname: user.shortname,
    gender: user.gender,
    birthdate: user.birthdate,
    measures: JSON.stringify(user.measures),
    external_id: user.external_id,
    mailingpref: user.mailingpref,
    unit_pref: JSON.stringify(user.unit_pref),
    preflang: user.preflang,
    timezone: user.timezone,
  };
  if (user.firstname) {
    params.firstname = user.firstname;
  }
  if (user.lastname) {
    params.lastname = user.lastname;
  }
  if (user.phonenumber) {
    params.phonenumber = user.phonenumber
  }
  if (user.goals) {
    params.goals = user.goals;
  }
  if (user.recovery_code) {
    params.recovery_code = user.recovery_code;
  }
  if (testmode !== null) {
    params.testmode = testmode;
  }
  params.signature = sign(params, CLIENT_SECRET);
  const { data } = await axios.post(API_ENDPOINT + 'v2/dropshipment', params);
  return data;
}

async function main() {
  const address   = {
    name: 'John Doe',
    company_name: 'Company',
    email: 'john.doe@example.com',
    telephone: '+551155256325',
    address1: '767 5th Ave',
    address2: 'Apt 101',
    city: 'Issy-les-Moulineaux',
    zip: '10153',
    state: 'NY',
    country: 'FR'
  };
  const products  = [
    {
      ean: '3700546707100',
      quantity: 1
    }
  ];
  const measures  = [
    { value: 190, unit: -2, type: MEASURE_TYPE_HEIGHT }, //1m90
    { value: 90, unit: 0, type:  MEASURE_TYPE_WEIGHT} //90kg
  ];
  const order     = {
    customer_ref_id: 'customer-ref-id', //the id of this order on your side
    address,
    products,
    force_address: true,
  };
  const unit_pref = { weight: UNIT_WEIGHT_KG, height: UNIT_HEIGHT_M, distance: UNIT_DISTANCE_KM, temperature: UNIT_TEMPERATURE_C };
  const mandatory_params_user = {
    mailingpref: 0,
    birthdate: 848166152,
    measures, // measurements should always be passed in kg/m
    gender: 0,
    preflang: 'en_US',
    unit_pref,
    email: 'john.doe@example.com',
    timezone: 'America/New_York',
    shortname: 'HNI',
    external_id: 'my-external-id' //id of this user on your side
  }
  const optional_params_user  = {
    firstname: 'John', //if not set, will take the same value as shortname
    lastname: 'Doe', //if not set, will take the same value as shortname
    phonenumber: '+551155256325', //Phone number in E.164 format.
    recovery_code: 'abcde12345',
    goals: null
  }
  const user = {...mandatory_params_user, ...optional_params_user};
  const now = Math.round((Date.now() / 1000));
  const response = await createUserOrder(await getNonce(now), order, user, TESTMODE_FAILED);
  console.log(JSON.stringify(response));
}

main();

module.exports = {
  createUserOrder
}

Response

{
"status": 0,
"body": {
"user": {
"code": "4ab52f3da1e1c8da346ec68ff26a45104c4859f6",
"external_id": "my-external-id"
},
"orders": [
{
"orderid": "123456",
"status": "SHIPPED",
"address": {
"name": "John Wayne",
"email": "john.wayne@example.com",
"city": "New York"
}
}
]
}
}