Example: Create Customer for Product using cURL

After a lot of trial-and-error (and reading this forum), I've figured out the proper cURL incantations to do the following:

  1. Create OAuth client credentials ("MyApp")
  2. Create a customer for an organization (org slug: "example-corp")
  3. Get a scoped access token for that customer ("user-email@example.com")
  4. Get a device claim code for that customer (product slug: "my-product-42")

I'm writing this just to put all the information in one place to pay-it-forward.

NOTE: The response data from each step feeds into the following step.

All response examples dated 4/1/2016 (no, there are no April fools jokes embedded here).


Create OAuth Client Credentials

curl -X POST -H "Authorization: Bearer 96xxxxxxxxxxxxxxxxxxxxxx65" -d name="MyApp" -d type=installed -d organization=example-corp https://api.particle.io/v1/clients

Note: The authorization header access token was taken from Particle Build.

{
   "ok": true,
   "client": {
     "name": "MyApp",
     "type": "installed",
     "id": "myapp-XXXX",
     "secret": "42xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx42",
     "org_id": "12xxxxxxxxxxxxxxxxxxxx34"
   }
 }

Create a Customer for an Organization

curl -X POST -u myapp-XXXX:42xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx42 -d email="user-email@example.com" -d password="password" https://api.particle.io/v1/orgs/example-corp/customers

 {
   "token_type": "bearer",
   "access_token": "ebxxxxxxxxxxxxxxxxxxxxxxxxxxx7b",
   "expires_in": 7776000,
   "refresh_token": "2bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx58"
 }

Create a Scoped Access Token for the Customer

curl -X POST -u myapp-XXXX:42xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx42 -d grant_type=client_credentials -d scope=customer=user-email@example.com https://api.particle.io/oauth/token

 {
   "token_type": "bearer",
   "access_token": "2fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx03",
   "expires_in": 7776000,
   "refresh_token": "20xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx94",
   "scope": "customer=user-email@example.com"
 }

Get a Device Claim Code for the Customer

curl -X POST -H "Authorization: Bearer 2fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx03" https://api.particle.io/v1/orgs/example-corp/products/my-product-42/device_claims

 {
   "claim_code": "b/Qadxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx9j0",
   "device_ids": [ ]
 }

And Now...
You should have everything you need at this point to put the Photon in SoftAP mode, set the new claim code, and configure the device for WiFi. Once the Photon connects to the cloud with the new claim code, the Particle cloud backend will know to associate the customer to the device.

Happy Programming!

2 Likes

Hello @brewnerd.

I cant get this commands to work. it always break at the -d
i guess im just using it wrong. can you give a hint on what im doing wrong?

curl -X POST -u myapp-XXXX:42xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx42 -d email="user-email@example.com" -d password=“password” https://api.particle.io/v1/orgs/example-corp/customers

C:\Program Files\cURL\bin>curl -X POST -u myapp-xxx -d email="email"
curl: no URL specified!
curl: try ‘curl --help’ or ‘curl --manual’ for more information

C:\Program Files\cURL\bin> -d password=“klofver” https://api.particle.io/v1/orgs/klofverskjold/customers

BR
Emilkl

Hello again.

i managed it was a copy paste issue :slight_smile:
However i cant create customer it seems.

im at the step create customer.
It says its successfull but i cant see anyone in my customer list in the dashboard, would you know why?

Can you please explain why a scoped access token is needed?

/BR
Emilkl

Hi there - I’m having the same issue. Following the steps above, but a customer is not listed in my dashboard despite all the commands apparently working properly in terminal. Did you ever figure out what the problem here was?

Hello Mattw.

Sorry i never did :frowning:
im busy with other things right now but i will probably give it a shoot later on.

Seems like they are doing a lot of uppgrades to the cloud right now thou so it will be intressting to see what will come.
I hope for more customer related things.

/Emilkl

@emilkl, No worries, I actually found a fix! Rather simple actually. In my case, the dashboard would only show customers who had devices registered to their account. We spun up a quick mobile app using the new Carthage Example, (you need to use a custom app to register customers/devices to your specific product) and as soon as we used it to claim a device, the customer’s name would pop up in the dashboard.

Hope that helps!

mattw

1 Like