Generate Password
Generates a random password that meets ECGrid complexity requirements, ready to use with Update Password or Create User.
Endpoint
POST /v2/users/generate-password
Request Body
| Field | Type | Required | Constraints | Description |
|---|---|---|---|---|
length | integer | No | Defaults to 12 | Desired length of the generated password |
{
"length": 16
}
Response
Returns the generated password string. The returned password is guaranteed to satisfy the ECGrid password complexity pattern:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z0-9]).+$
{
"success": true,
"data": "Xk7#mQpL2rNv!dYw"
}
Code Examples
- cURL
- C#
- Java
- Node.js
- Python
curl -X POST "https://rest.ecgrid.io/v2/users/generate-password" \
-H "X-API-Key: $ECGRID_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "length": 16 }'
// .NET 10 — Generate a compliant random password using IHttpClientFactory
public async Task<string?> GeneratePasswordAsync(
IHttpClientFactory httpClientFactory,
int length = 12)
{
var http = httpClientFactory.CreateClient("ECGridRest");
var requestBody = new { length };
var response = await http.PostAsJsonAsync(
"https://rest.ecgrid.io/v2/users/generate-password",
requestBody);
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadFromJsonAsync<ApiResponse<string>>();
return result?.Data;
}
import java.net.URI;
import java.net.http.*;
String apiKey = System.getenv("ECGRID_API_KEY");
String body = "{ \"length\": 16 }";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://rest.ecgrid.io/v2/users/generate-password"))
.header("X-API-Key", apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(
request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
const apiKey = process.env.ECGRID_API_KEY;
const url = 'https://rest.ecgrid.io/v2/users/generate-password';
const response = await fetch(url, {
method: 'POST',
headers: {
'X-API-Key': apiKey,
'Content-Type': 'application/json',
},
body: JSON.stringify({ "length": 16 }),
});
const data = await response.json();
console.log(data);
import os, requests
api_key = os.environ["ECGRID_API_KEY"]
headers = {"X-API-Key": api_key}
url = "https://rest.ecgrid.io/v2/users/generate-password"
response = requests.post(
url,
json={ "length": 16 },
headers=headers,
)
response.raise_for_status()
print(response.json())
See Also
- Update Password — apply the generated password to a user account
- Create User — supply the generated password when creating a new user