Step 15: Set user device (SMS)
POST {{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices
The POST {{apiPath}}/environments/{{envID}}/users/{{userID}}/devices operation creates an SMS MFA device and associates this device with the specified user. The user must have an associated device to complete an MFA authentication action. This action defines an SMS device so that the user can receive a one-time passcode by text message.
Example Request
-
cURL
-
C#
-
Go
-
HTTP
-
Java
-
jQuery
-
NodeJS
-
Python
-
PHP
-
Ruby
-
Swift
curl --location --globoff '{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer {{accessToken}}' \
--data '{
"type": "SMS",
"phone": "{{phone}}",
"policy": {
"id": "{{deviceAuthPolicyID}}"
}
}'
var options = new RestClientOptions("{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices")
{
MaxTimeout = -1,
};
var client = new RestClient(options);
var request = new RestRequest("", Method.Post);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer {{accessToken}}");
var body = @"{" + "\n" +
@" ""type"": ""SMS""," + "\n" +
@" ""phone"": ""{{phone}}""," + "\n" +
@" ""policy"": {" + "\n" +
@" ""id"": ""{{deviceAuthPolicyID}}""" + "\n" +
@" }" + "\n" +
@"}";
request.AddStringBody(body, DataFormat.Json);
RestResponse response = await client.ExecuteAsync(request);
Console.WriteLine(response.Content);
package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices"
method := "POST"
payload := strings.NewReader(`{
"type": "SMS",
"phone": "{{phone}}",
"policy": {
"id": "{{deviceAuthPolicyID}}"
}
}`)
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer {{accessToken}}")
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
POST /environments/{{envID}}/users/{{MFAUserID}}/devices HTTP/1.1
Host: {{apiPath}}
Content-Type: application/json
Authorization: Bearer {{accessToken}}
{
"type": "SMS",
"phone": "{{phone}}",
"policy": {
"id": "{{deviceAuthPolicyID}}"
}
}
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\n \"type\": \"SMS\",\n \"phone\": \"{{phone}}\",\n \"policy\": {\n \"id\": \"{{deviceAuthPolicyID}}\"\n }\n}");
Request request = new Request.Builder()
.url("{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer {{accessToken}}")
.build();
Response response = client.newCall(request).execute();
var settings = {
"url": "{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json",
"Authorization": "Bearer {{accessToken}}"
},
"data": JSON.stringify({
"type": "SMS",
"phone": "{{phone}}",
"policy": {
"id": "{{deviceAuthPolicyID}}"
}
}),
};
$.ajax(settings).done(function (response) {
console.log(response);
});
var request = require('request');
var options = {
'method': 'POST',
'url': '{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices',
'headers': {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{accessToken}}'
},
body: JSON.stringify({
"type": "SMS",
"phone": "{{phone}}",
"policy": {
"id": "{{deviceAuthPolicyID}}"
}
})
};
request(options, function (error, response) {
if (error) throw new Error(error);
console.log(response.body);
});
import requests
import json
url = "{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices"
payload = json.dumps({
"type": "SMS",
"phone": "{{phone}}",
"policy": {
"id": "{{deviceAuthPolicyID}}"
}
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{accessToken}}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
<?php
require_once 'HTTP/Request2.php';
$request = new HTTP_Request2();
$request->setUrl('{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices');
$request->setMethod(HTTP_Request2::METHOD_POST);
$request->setConfig(array(
'follow_redirects' => TRUE
));
$request->setHeader(array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer {{accessToken}}'
));
$request->setBody('{\n "type": "SMS",\n "phone": "{{phone}}",\n "policy": {\n "id": "{{deviceAuthPolicyID}}"\n }\n}');
try {
$response = $request->send();
if ($response->getStatus() == 200) {
echo $response->getBody();
}
else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
$response->getReasonPhrase();
}
}
catch(HTTP_Request2_Exception $e) {
echo 'Error: ' . $e->getMessage();
}
require "uri"
require "json"
require "net/http"
url = URI("{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices")
http = Net::HTTP.new(url.host, url.port);
request = Net::HTTP::Post.new(url)
request["Content-Type"] = "application/json"
request["Authorization"] = "Bearer {{accessToken}}"
request.body = JSON.dump({
"type": "SMS",
"phone": "{{phone}}",
"policy": {
"id": "{{deviceAuthPolicyID}}"
}
})
response = http.request(request)
puts response.read_body
let parameters = "{\n \"type\": \"SMS\",\n \"phone\": \"{{phone}}\",\n \"policy\": {\n \"id\": \"{{deviceAuthPolicyID}}\"\n }\n}"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "{{apiPath}}/environments/{{envID}}/users/{{MFAUserID}}/devices")!,timeoutInterval: Double.infinity)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("Bearer {{accessToken}}", forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
return
}
print(String(data: data, encoding: .utf8)!)
}
task.resume()
Example Response
201 Created
{
"_links": {
"self": {
"href": "https://api.pingone.com/v1/environments/abfba8f6-49eb-49f5-a5d9-80ad5c98f9f6/users/8cc45fa5-214e-429b-b91c-e900075c3e43/devices/1237625c-12f9-4ab1-8656-351142b6c100"
},
"environment": {
"href": "https://api.pingone.com/v1/environments/abfba8f6-49eb-49f5-a5d9-80ad5c98f9f6"
},
"user": {
"href": "https://api.pingone.com/v1/environments/abfba8f6-49eb-49f5-a5d9-80ad5c98f9f6/users/8cc45fa5-214e-429b-b91c-e900075c3e43"
}
},
"id": "1237625c-12f9-4ab1-8656-351142b6c100",
"environment": {
"id": "abfba8f6-49eb-49f5-a5d9-80ad5c98f9f6"
},
"user": {
"id": "8cc45fa5-214e-429b-b91c-e900075c3e43"
},
"type": "SMS",
"status": "ACTIVE",
"createdAt": "2021-08-04T22:58:45.945Z",
"updatedAt": "2021-08-04T22:58:45.945Z",
"phone": "+15125550101"
}