Skip to main content
POST
/
api
/
register
Register User
curl --request POST \
  --url https://api.example.com/api/register \
  --header 'Content-Type: <content-type>' \
  --header 'x-api-key: <x-api-key>' \
  --data '
{
  "name": "<string>",
  "email": "<string>",
  "password": "<string>"
}
'
{
  "status": "<string>",
  "message": "<string>",
  "data": {
    "token": "<string>"
  }
}

Endpoint

POST http://localhost:5000/api/register

Headers

x-api-key
string
required
Your API key for accessing the API
Content-Type
string
required
Must be application/json

Request Body

name
string
required
The full name of the user
email
string
required
The user’s email address. Must be unique and not already registered.
password
string
required
The user’s password. Will be hashed using bcrypt with 10 salt rounds before storage.

Response

status
string
Response status - “OK” for success
message
string
Response message - “User Registered” on success
data
object

Code Example

Here’s the actual implementation from the controller:
export const register = async (req, res) => {
  const { name, email, password } = req.body;

  if (!name || !email || !password) {
    return errorResponse({
      res,
      statusCode: 400,
      message: "All fields are required",
    });
  }

  try {
    const [existingUser] = await db.execute(
      "SELECT * FROM users WHERE email = ?",
      [email]
    );
    if (existingUser.length > 0) {
      return errorResponse({
        res,
        statusCode: 409,
        message: "Email is already registered",
      });
    }

    const hashedPassword = await bcrypt.hash(password, 10);

    const [result] = await db.execute(
      "INSERT INTO users (name, email, password) VALUES (?, ?, ?)",
      [name, email, hashedPassword]
    );

    const token = jwt.sign(
      { id: result.insertId, email, name },
      process.env.JWT_SECRET,
      { expiresIn: "8h" }
    );

    successResponse({
      res,
      statusCode: 201,
      message: "User Registered",
      data: { token },
    });
  } catch (err) {
    errorResponse({ res, statusCode: 500, message: "Internal Server Error" });
  }
};

Request Example

curl -X POST http://localhost:5000/api/register \
  -H "x-api-key: your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "[email protected]",
    "password": "securepassword123"
  }'

Response Examples

Success Response (201)

{
  "status": "OK",
  "message": "User Registered",
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
  }
}

Error Responses

Missing Fields (400)

{
  "status": "ERROR",
  "message": "All fields are required",
  "data": null
}

Invalid API Key (403)

{
  "status": "ERROR",
  "message": "Invalid API Key",
  "data": null
}

Email Already Registered (409)

{
  "status": "ERROR",
  "message": "Email is already registered",
  "data": null
}

Server Error (500)

{
  "status": "ERROR",
  "message": "Internal Server Error",
  "data": null
}

Token Usage

After successful registration, use the returned JWT token for authenticated endpoints by including it in the Authorization header:
Authorization: Bearer <token>
The token expires after 8 hours and contains the following payload:
{
  "id": 1,
  "email": "[email protected]",
  "name": "John Doe",
  "iat": 1234567890,
  "exp": 1234596690
}

Build docs developers (and LLMs) love