> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.tree-nation.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.tree-nation.com/_mcp/server.

# Buy credit

POST https://tree-nation.com/api/credit
Content-Type: application/json

Purchase credit for a planter account.
Credit is denominated in EUR and used to fund future tree plantings.
The API user must have `can_refill_credit = true`.


Reference: https://docs.tree-nation.com/api-reference/plant/buy-credit

## Authentication

- `Authorization` header (bearer token, required) — Bearer token required for all User API endpoints. Routes are additionally guarded by an `active` middleware (account must be active).

## Request

### Body (application/json)

This endpoint expects a BuyCreditRequest.

- `planter_id` (integer, required) — ID of the user whose credit balance is topped up.
- `amount` (double, required) — Credit amount to add (in EUR). If `credit_unpaid_limit` is set on the API user, cumulative unpaid credit plus this amount must not exceed that limit.

## Response

### 200

Credit result. Check `status` field — HTTP 200 is returned even for business-logic errors.

- `status` (enum, optional)
  - Allowed values: `ok`, `error`
- `payment_id` (integer, optional) — ID of the created payment record (present on success).
- `errorCode` (enum, optional, nullable)
  - Allowed values: `cant_refill`, `data`, `planter`, `credit_limit`
- `errorMessage` (string, optional, nullable)

## Errors

### 401 Unauthorized Error

Missing or invalid token.

- `any`

### 403 Forbidden Error

Account is inactive.

- `any`

## Examples

**Request**

```json
{
  "planter_id": 1024,
  "amount": 1500
}
```

**Response**

```json
{
  "status": "ok",
  "payment_id": 4587,
  "errorCode": "",
  "errorMessage": ""
}
```

**SDK Code**

```python
import requests

url = "https://tree-nation.com/api/credit"

payload = {
    "planter_id": 1024,
    "amount": 1500
}
headers = {
    "Authorization": "Bearer <token>",
    "Content-Type": "application/json"
}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
```

```javascript
const url = 'https://tree-nation.com/api/credit';
const options = {
  method: 'POST',
  headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
  body: '{"planter_id":1024,"amount":1500}'
};

try {
  const response = await fetch(url, options);
  const data = await response.json();
  console.log(data);
} catch (error) {
  console.error(error);
}
```

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://tree-nation.com/api/credit"

	payload := strings.NewReader("{\n  \"planter_id\": 1024,\n  \"amount\": 1500\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Authorization", "Bearer <token>")
	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
```

```ruby
require 'uri'
require 'net/http'

url = URI("https://tree-nation.com/api/credit")

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n  \"planter_id\": 1024,\n  \"amount\": 1500\n}"

response = http.request(request)
puts response.read_body
```

```java
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

HttpResponse<String> response = Unirest.post("https://tree-nation.com/api/credit")
  .header("Authorization", "Bearer <token>")
  .header("Content-Type", "application/json")
  .body("{\n  \"planter_id\": 1024,\n  \"amount\": 1500\n}")
  .asString();
```

```php
<?php
require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://tree-nation.com/api/credit', [
  'body' => '{
  "planter_id": 1024,
  "amount": 1500
}',
  'headers' => [
    'Authorization' => 'Bearer <token>',
    'Content-Type' => 'application/json',
  ],
]);

echo $response->getBody();
```

```csharp
using RestSharp;

var client = new RestClient("https://tree-nation.com/api/credit");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer <token>");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", "{\n  \"planter_id\": 1024,\n  \"amount\": 1500\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
```

```swift
import Foundation

let headers = [
  "Authorization": "Bearer <token>",
  "Content-Type": "application/json"
]
let parameters = [
  "planter_id": 1024,
  "amount": 1500
] as [String : Any]

let postData = JSONSerialization.data(withJSONObject: parameters, options: [])

let request = NSMutableURLRequest(url: NSURL(string: "https://tree-nation.com/api/credit")! as URL,
                                        cachePolicy: .useProtocolCachePolicy,
                                    timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error as Any)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()
```