Skip to main content

POST /api/transactions

Creates a new currency exchange transaction for the authenticated user. The endpoint automatically calculates commissions based on the transaction amount and validates the exchange rate against the current market rate with a tolerance of 0.35 Bs.

Authentication

This endpoint requires authentication. Include a valid JWT token in the Authorization header:
Authorization: Bearer <your_token>

Request Body

transaction_type
string
required
The type of transaction. Must be either "Comprar" (Buy) or "Vender" (Sell).
  • Comprar: User buys USD, commission is added to the amount
  • Vender: User sells USD, commission is deducted from the amount
amount_usd
number
required
The base amount in USD for the transaction. Must be a positive number.The commission is automatically calculated based on this amount:
  • 11 - 9.99: $0.80 commission
  • 1010 - 14.99: $1.00 commission
  • 1515 - 25: $1.40 commission
  • Above 25:25: 1.40 + (0.08×(amount0.08 × (amount - 25))
rate_bs
number
required
The exchange rate in Bolivares (Bs) per USD. This rate is validated against the current market rate stored in the system.The provided rate must be within 0.35 Bs tolerance of the current rate, otherwise the request will be rejected with a 400 error.
payment_reference
string
required
A unique payment reference number for this transaction. This field is checked for duplicates to prevent processing the same payment twice.If a transaction with this reference already exists, the request will fail with a 409 Conflict error.
type_pay
string
The payment method used for this transaction (e.g., “Pago Móvil”, “Transferencia”, “Zelle”).
recipient_account
string
The account information for the recipient. Format depends on the payment method.

Example Request

curl -X POST http://localhost:3001/api/transactions \
  -H "Authorization: Bearer your_jwt_token" \
  -H "Content-Type: application/json" \
  -d '{
    "transaction_type": "Comprar",
    "amount_usd": 50.00,
    "rate_bs": 36.50,
    "payment_reference": "REF123456789",
    "type_pay": "Pago Móvil",
    "recipient_account": "0414-1234567"
  }'

Response

message
string
Success message confirming the transaction was created.
transaction
object
The created transaction object with all calculated fields.

Success Response Example

{
  "message": "Transacción creada exitosamente",
  "transaction": {
    "id": 42,
    "user_id": 15,
    "transaction_type": "Comprar",
    "amount_usd": 50.00,
    "commission_usd": 3.40,
    "total_usd": 53.40,
    "rate_bs": 36.50,
    "total_bs": 1949.10,
    "payment_reference": "REF123456789",
    "status": "Pendiente",
    "type_pay": "Pago Móvil",
    "recipient_account": "0414-1234567"
  }
}

Error Responses

400 Bad Request
object
Returned when required fields are missing, rate validation fails, or commission exceeds the amount for sell transactions.
401 Unauthorized
object
Returned when the authentication token is missing, invalid, or expired.
{
  "message": "Authentication required"
}
409 Conflict
object
Returned when the provided payment_reference has already been used in another transaction.
{
  "message": "La referencia de pago ya fue utilizada"
}
500 Internal Server Error
object
Returned when an unexpected server error occurs.
{
  "message": "Error del servidor"
}

Commission Calculation Details

The commission structure is tiered based on the transaction amount:
Amount RangeCommission
1.001.00 - 9.99$0.80 (flat)
10.0010.00 - 14.99$1.00 (flat)
15.0015.00 - 25.00$1.40 (flat)
Above $25.001.40+(1.40 + (0.08 × (amount - $25))

Examples:

  • **8.00:Commission=8.00**: Commission = 0.80
  • **12.00:Commission=12.00**: Commission = 1.00
  • **20.00:Commission=20.00**: Commission = 1.40
  • **30.00:Commission=30.00**: Commission = 1.40 + (0.08×5)=0.08 × 5) = 1.80
  • **50.00:Commission=50.00**: Commission = 1.40 + (0.08×25)=0.08 × 25) = 3.40

Rate Validation

The system validates that the exchange rate provided by the client is current and hasn’t changed significantly:
  1. The server maintains a current exchange rate (fetched from external services)
  2. When a transaction is created, the rate_bs is compared to the server’s rate
  3. If the absolute difference is greater than 0.35 Bs, the transaction is rejected
  4. This prevents transactions from being processed with stale or incorrect rates

Duplicate Prevention

To prevent accidental duplicate transactions, the payment_reference field is checked against all existing transactions. If a match is found, the request returns a 409 Conflict error. This ensures that each payment reference can only be used once across the entire system.

Build docs developers (and LLMs) love