Skip to main content
POST
/
registrar
User Registration
curl --request POST \
  --url https://api.example.com/registrar \
  --header 'Content-Type: application/json' \
  --data '
{
  "email": "<string>",
  "password": "<string>",
  "nombre": "<string>",
  "apellidos": "<string>"
}
'
{
  "mensaje": "<string>",
  "exito": true
}

Endpoint

POST /registrar
Creates a new user account with email, password, and optional name information.

Request Body

The request uses the UsuarioRegistro Pydantic model:
email
string
required
User’s email address (used as unique identifier)
password
string
required
User’s password (stored as plain text in current implementation)
nombre
string
User’s first name (optional, defaults to null)
apellidos
string
User’s last name (optional, defaults to null)

Response

mensaje
string
Status message describing the result
exito
boolean
Indicates whether registration was successful

Success Response

{
  "mensaje": "Usuario creado",
  "exito": true
}

Error Responses

Duplicate User (409-style):
{
  "mensaje": "El usuario ya existe",
  "exito": false
}
Database Error:
{
  "mensaje": "Error: [error details]",
  "exito": false
}

Error Handling

The endpoint handles duplicate email registrations gracefully by catching psycopg2.IntegrityError and returning exito: false instead of raising an HTTP exception. All database errors result in a rollback.

Examples

cURL

curl -X POST http://localhost:8000/registrar \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "password": "securepass123",
    "nombre": "Juan",
    "apellidos": "García"
  }'

Python (requests)

import requests

url = "http://localhost:8000/registrar"
data = {
    "email": "[email protected]",
    "password": "securepass123",
    "nombre": "Juan",
    "apellidos": "García"
}

response = requests.post(url, json=data)
result = response.json()

if result["exito"]:
    print(f"Registration successful: {result['mensaje']}")
else:
    print(f"Registration failed: {result['mensaje']}")

JavaScript (axios)

const axios = require('axios');

const registerUser = async () => {
  try {
    const response = await axios.post('http://localhost:8000/registrar', {
      email: '[email protected]',
      password: 'securepass123',
      nombre: 'Juan',
      apellidos: 'García'
    });
    
    if (response.data.exito) {
      console.log(`Registration successful: ${response.data.mensaje}`);
    } else {
      console.log(`Registration failed: ${response.data.mensaje}`);
    }
  } catch (error) {
    console.error('Request failed:', error.message);
  }
};

registerUser();

Implementation Notes

  • Email is used as the primary unique identifier (enforced by database constraint)
  • Passwords are stored in plain text (⚠️ security concern for production)
  • nombre and apellidos fields default to null if not provided
  • Database connection errors return HTTP 500
  • The endpoint performs a rollback on any database error to maintain data integrity

Build docs developers (and LLMs) love