function sendRequestsSequentially() { // 1. Create User Payload const userPayload = { login: "user5god1", language: "en", contact: { email: "carbontest555+1000@gmail.com", firstname: "user5god1", lastname: "1", phone: "", title: null, types: null }, business_types: [], tenant_id: "546d92c4-533d-412c-86df-557f34a74b3c", idp_id: "11111111-1111-1111-1111-111111111111" }; const xhr1 = new XMLHttpRequest(); xhr1.open("POST", "/api/2/users", true); xhr1.setRequestHeader("Content-Type", "application/json"); xhr1.onreadystatechange = () => { if (xhr1.readyState === 4) { console.log("1️⃣ Create User:", xhr1.status, xhr1.responseText); if (xhr1.status >= 200 && xhr1.status < 300) { let response; try { response = JSON.parse(xhr1.responseText); } catch { console.error("Failed to parse JSON response"); return; } const userId = response.id; if (!userId) { console.error("User ID not found in response"); return; } // 2. Update Access Policies using userId const xhr2 = new XMLHttpRequest(); xhr2.open("PUT", `/api/2/users/${userId}/access_policies`, true); xhr2.setRequestHeader("Content-Type", "application/json"); xhr2.onreadystatechange = () => { if (xhr2.readyState === 4) { console.log("2️⃣ Update Access Policies:", xhr2.status, xhr2.responseText); // 3. Activate Email const xhr3 = new XMLHttpRequest(); xhr3.open("POST", "/api/1/actions/mail/activate/", true); xhr3.setRequestHeader("Content-Type", "application/json"); xhr3.onreadystatechange = () => { if (xhr3.readyState === 4) { console.log("3️⃣ Activate Email:", xhr3.status, xhr3.responseText); } }; xhr3.send(JSON.stringify({ login: "user3god1", email: "carbontest555+100@gmail.com" })); } }; xhr2.send(JSON.stringify({ items: [{ role_id: "partner_admin", id: "00000000-0000-0000-0000-000000000000", issuer_id: "546d92c4-533d-412c-86df-557f34a74b3c", tenant_id: "546d92c4-533d-412c-86df-557f34a74b3c", trustee_id: userId, // dynamic replacement here trustee_type: "user", version: 0 }] })); } else { console.error("Failed to create user, stopping further requests"); } } }; xhr1.send(JSON.stringify(userPayload)); } sendRequestsSequentially();