JS script to mass deactive SIMs

@rickkas7 I’m trying to use the GitHub - rickkas7/particle-node-api-examples: Example code for using the Particle API from node.js (Javascript) to mass deactivate sims in a product and I’m getting the following error. Do you know what may be wrong?

De-activate SIMs from ICCID
--------------------------------------------------------------------------------
productId=XXXX
country=US
simICCIDs.txt
XXXXX
request {
  iccids: [ 'XXXXX' ],
  product: XXXX,
  auth: 'xxxxxxx'
}
error deactivating SIM Error: HTTP error 400 from https://api.particle.io/v1/products/XXX/sims/undefined
    at C:\Users\wesne\Desktop\Atom Projects\product-deactivate-sims\node_modules\particle-api-js\lib\Agent.js:204:19
    at Request.callback (C:\Users\wesne\Desktop\Atom Projects\product-deactivate-sims\node_modules\superagent\lib\node\index.js:728:3)
    at C:\Users\wesne\Desktop\Atom Projects\product-deactivate-sims\node_modules\superagent\lib\node\index.js:916:18
    at IncomingMessage.<anonymous> (C:\Users\wesne\Desktop\Atom Projects\product-deactivate-sims\node_modules\superagent\lib\node\parsers\json.js:19:7)
    at IncomingMessage.emit (node:events:539:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  statusCode: 400,
  errorDescription: 'HTTP error 400 from https://api.particle.io/v1/products/XXXX/sims/undefined',
  shortErrorDescription: undefined,
  error: Error: Bad Request
      at Request.callback (C:\Users\wesne\Desktop\Atom Projects\product-deactivate-sims\node_modules\superagent\lib\node\index.js:706:15)
      at C:\Users\wesne\Desktop\Atom Projects\product-deactivate-sims\node_modules\superagent\lib\node\index.js:916:18
      at IncomingMessage.<anonymous> (C:\Users\wesne\Desktop\Atom Projects\product-deactivate-sims\node_modules\superagent\lib\node\parsers\json.js:19:7)
      at IncomingMessage.emit (node:events:539:35)
      at endReadableNT (node:internal/streams/readable:1345:12)
      at processTicksAndRejections (node:internal/process/task_queues:83:21) {
    status: 400,
    response: Response {
      _events: [Object: null prototype] {},
      _eventsCount: 0,
      _maxListeners: undefined,
      res: [IncomingMessage],
      request: [Request],
      req: [ClientRequest],
      text: '{"ok":false,"code":400,"error":"ICCID undefined does not pass luhn validation."}',
      body: [Object],
      files: undefined,
      buffered: true,
      headers: [Object],
      header: [Object],
      statusCode: 400,
      status: 400,
      statusType: 4,
      info: false,
      ok: false,
      redirect: false,
      clientError: true,
      serverError: false,
      error: [Error],
      created: false,
      accepted: false,
      noContent: false,
      badRequest: true,
      unauthorized: false,
      notAcceptable: false,
      forbidden: false,
      notFound: false,
      unprocessableEntity: false,
      type: 'application/json',
      charset: 'utf-8',
      links: {},
      setEncoding: [Function: bound ],
      redirects: [],
      [Symbol(kCapture)]: false
    }
  },
  body: {
    ok: false,
    code: 400,
    error: 'ICCID undefined does not pass luhn validation.'
  }
}
Press any key to return to the menu. . .

Which script did you start from, the product-active-sims? In any case, the problem is probably here:

https://api.particle.io/v1/products/XXX/sims/undefined

The undefined at the end should actually be an ICCID. With activate, you can do it in bulk by passing a list of SIMs to activate in the iccids parameter (plural).

But for both deactivate and release ownership you need to call it once per SIM, so you need to loop over the array of iccids and make a single call with a single iccid in the iccid parameter (singular).

@rickkas7 I also tried the deactivate-developer-sims and updated it to include

var req = { iccids:iccids, product:productId, auth:config.get('AUTH_TOKEN') };
``
but also received the below error. 



De-activate SIMs from ICCID

productId=XXX
simICCIDs.txt
deactivating xxxx…
failed XXX
Error: HTTP error 400 from https://api.particle.io/v1/products/XXX/sims/undefined
at C:\Users\wesne\Desktop\Atom Projects\deactivate-developer-sims\node_modules\particle-api-js\lib\Agent.js:189:19
at Request.callback (C:\Users\wesne\Desktop\Atom Projects\deactivate-developer-sims\node_modules\superagent\lib\node\index.js:631:3)
at C:\Users\wesne\Desktop\Atom Projects\deactivate-developer-sims\node_modules\superagent\lib\node\index.js:795:18
at IncomingMessage. (C:\Users\wesne\Desktop\Atom Projects\deactivate-developer-sims\node_modules\superagent\lib\node\parsers\json.js:16:7)
at IncomingMessage.emit (node:events:539:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
statusCode: 400,
errorDescription: ‘HTTP error 400 from https://api.particle.io/v1/products/XXX/sims/undefined’,
shortErrorDescription: undefined,
error: Error: Bad Request

I got it to work by changing it to the below:

const config = require('./config');

var Particle = require('particle-api-js');
var particle = new Particle();

// const argv = require('yargs').argv

const argv = require('yargs')
	.usage('Usage: $0 [options] --productId=NN <sim_file...>')
	.alias('p', 'productId')
	.nargs('p', 1)
	.describe('p', 'product ID to add to (required)')
	.argv;

const fs = require('fs');

var iccids = [];

var productId = parseInt(argv.p);
if (isNaN(productId)) {
	console.log("--productId=NNN required");
	return 1;	
}
console.log("productId=" + productId);


if (argv._.length == 0) {
	console.log("file of SIM ICCIDs is required");
	return 1;
}

for(var ii = 0; ii < argv._.length; ii++) {
	processFile(argv._[ii]);
}

// You must set AUTH_TOKEN in an enviroment variable!
// var req = { iccid: iccid, product: productId, auth:config.get('AUTH_TOKEN') };

deactivateNext();


function processFile(name) {
	console.log(name);
	
	var data = fs.readFileSync(name, 'utf8');
		
	var re = /[0-9]{19}/;
	var a = data.split("\n");
	for(var ii = 0; ii < a.length; ii++) {
		var iccid = a[ii].trim();
		if (re.test(iccid)) {
			iccids.push(iccid);
		}
	}
}

function deactivateNext() {
	if (iccids.length == 0) {
		return;
	}
	var iccid = iccids.shift();
	console.log("deactivating " + iccid + "...");
	
	particle.deactivateSIM({ iccid: iccid, product: productId, auth:config.get('AUTH_TOKEN') }).then(
			function(data) {
				console.log("  success!");
				deactivateNext();
			},
			function(err) {
				console.log("  failed " + iccid, err);
				deactivateNext();
			}
			);
	
} 
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.