API basics
How to connect to the Paytrim API, authenticate your requests and handle responses and errors.
Connect securely
The API is only available over HTTPS with TLS 1.2 or newer. This protects your customers' data on its way between your server and Paytrim.
Most modern HTTP clients support TLS 1.2 by default. If you use an old library or an old version of a programming language, update it.
All requests go to https://api.pay.paytrim.com, followed by the path of the endpoint, such as /v1/orders/.
Authenticate
Authenticate every request with HTTP Basic authentication. Your project's API key is the username, and the password is empty.
Most HTTP clients handle Basic authentication for you. With cURL:
curl https://api.pay.paytrim.com/v1/merchants/self/projects/self/ \
--user "$API_KEY:"If your client doesn't support Basic authentication, build the Authorization header yourself: encode your API key followed by a colon (:) with Base64. For the API key hunter2, you encode hunter2:, which results in this header:
Authorization: Basic aHVudGVyMjo=Keep your API key secret
The API key gives full access to your project. Only use it from your server. Never put it in a browser, mobile app or public repository.
HTTP status codes
The API uses standard HTTP status codes.
| Code | Name | Meaning |
|---|---|---|
200 | OK | The request succeeded. The response body contains the requested data. |
201 | Created | The resource has been created. |
202 | Accepted | The request has been accepted, but processing isn't finished yet. |
204 | No Content | The request succeeded. The response has no body. |
400 | Bad Request | The request is invalid. Check the parameters and the body. |
401 | Unauthorized | Authentication failed or is missing. Check your API key. |
403 | Forbidden | You don't have permission for this action. |
404 | Not Found | The resource doesn't exist. |
409 | Conflict | The request conflicts with the current state of the resource. |
422 | Unprocessable Entity | The request is well-formed, but validation failed. |
429 | Too Many Requests | You've exceeded the rate limit. Try again later. |
500 | Internal Server Error | Something unexpected went wrong on the server. |
502 | Bad Gateway | An upstream service returned an invalid response. |
503 | Service Unavailable | The service is temporarily unavailable. Try again later. |
504 | Gateway Timeout | An upstream service didn't respond in time. |
Handle 2xx responses
A 2xx response means Paytrim received and accepted your request. It doesn't mean the payment succeeded: check the status in the response.
For example, when you start a card payment:
- The API responds with
200 OKor201 Created. Paytrim has your request and processes the payment, but the outcome isn't known yet. - The card issuer declines the card. No money is collected.
- Paytrim sends a webhook because the order status changed.
- You retrieve the order. The
statusiserror, and thereasonfield briefly explains why. See Error reasons.
Handle 4xx and 5xx responses
Paytrim doesn't accept or store your request when:
- the request doesn't pass validation;
- the request violates a security constraint;
- the request violates a configuration constraint, such as a payment method that isn't active for your project;
- an internal error occurs at Paytrim.
You receive an error response that describes the problem. Handle it as an exception in your code. Rejected requests aren't charged.
Error response
An error response contains an error object:
{
"error": {
"status": 400,
"type": "RefundError",
"value": "Cannot refund more (500) than the remaining order amount (0)"
}
}| Field | Type | Description |
|---|---|---|
status | Integer | The HTTP status code. |
type | String | The type of error. See the table below. |
value | String | A short explanation of the problem. |
Common error types:
| Type | Meaning |
|---|---|
ValidationError | The request doesn't match the API schema, for example a missing or invalid field. |
MissingInformationToCreateTransaction | Information needed to create the transaction is missing. |
TransactionAlreadyStarted | The transaction has already started and can't be started again. |
CanNotCancelTransaction | The transaction can't be cancelled in its current state. |
RefundError | The refund isn't possible, for example because it exceeds the remaining amount. |
FailedToFetchTransactionInformation | The transaction information couldn't be retrieved. |
ServerError | An unexpected error occurred at Paytrim. |