# Enter one of your students for an exam

- Endpoint: `POST /v1/{organizationId}/application`
- Production: `https://api.main-team.org/v1/{organizationId}/application`
- Sandbox: `https://apisnd.main-team.org/v1/{organizationId}/application`
- Operation: `createApplication` (Applications)
- Authentication: `Authorization: Bearer <token>`, a short-lived token you sign with your API key and secret
- Permission: `application/create:$org:$ID`

## Description

Enters one of your students for one exam in this organization, and creates the payment record the application is paid through.

**Before you call it:** register the student (`registerStudent`), have them sign in to this organization once through a sign-in link (`createSigninLink`), which creates the organization’s own record of them, and pick the exam from `listAvailableExams`: a leaf’s `matchedExam._id` is the `examId` to send. An exam that list leaves out for this student is refused here too.

**Checks, in order.** The first that fails decides the answer.

1. The student is yours (`404`). Your students are the ones your account registered; anyone else’s answers like an unknown id.
2. The organization holds a record of the student (`409`).
3. The exam exists in this organization (`404`).
4. The exam is offered to this student: it is open, and it fits their grade (`400` for a student with no grade), their country and has a language (`409`, naming the rule).
5. The student already has this exact exam: `200` "Application already exists." with that application, and nothing is created.
6. The student holds no other exam in the same category on the same sitting (`409`).

**Side effects.** A payment record for the exam’s `price`: `paid` with `amount` 0 for a free exam, `pending` otherwise. This API never takes or refunds money. An application for a make-up sitting is removed automatically 6 hours after it is made (see `removeAfter`).

**Safe to retry** for the same student and exam: a repeat answers `200` with the application already there. Only while the exam is still offered to the student, though, because check 4 runs first: once the sitting date has passed, a repeat answers `409` and leaves the existing application as it was.

`data` is the application as stored. `exam`, `payment` and `user` are ids, and `user` is the organization’s own id for the student, not your `studentId`; `getApplication` resolves them.

## Parameters

| Name | In | Required | Type | Description |
| --- | --- | --- | --- | --- |
| `organizationId` | path | yes | string | The organization’s `_id`: 24 hexadecimal digits, as `listOrganizations` (`GET /v1/organization`) lists it. |

## Request body

Required fields: `examId`, `studentId`.

```json
{
  "examId": "6650a1b2c3d4e5f6a7b8c9e1",
  "studentId": "6650a1b2c3d4e5f6a7b8c9d0"
}
```

## Response

`200` Nothing new had to be created: `message` is "Application already exists.".

```json
{
  "success": true,
  "message": "Application already exists.",
  "pagination": {
    "page": 1,
    "limit": 20,
    "total": 57,
    "totalPages": 3
  },
  "data": {
    "_id": "6650a1b2c3d4e5f6a7b8c9e5",
    "exam": "6650a1b2c3d4e5f6a7b8c9e1",
    "user": "6650a1b2c3d4e5f6a7b8c9e9",
    "payment": "6650a1b2c3d4e5f6a7b8c9e6",
    "partners": [
      {
        "user": "6650a1b2c3d4e5f6a7b8c9d6",
        "accepted": true
      }
    ],
    "participated": false,
    "examStart": "2026-11-14T10:04:12.000Z",
    "examSubmitted": false,
    "submitDate": "2026-11-14T11:12:40.000Z",
    "simulationStarted": false,
    "simulationStart": "2026-11-07T10:00:00.000Z",
    "simulationSubmitted": false,
    "removeAfter": "2026-09-02T14:05:00.000Z",
    "uuid": "a3f-09c-7e1",
    "createdAt": "2026-09-01T09:30:00.000Z",
    "updatedAt": "2026-09-02T14:05:00.000Z"
  }
}
```

## Errors

| Status | Code | When |
| --- | --- | --- |
| 400 | [`bad_request`](https://hub.main-team.org/api/errors#bad_request) | The body is not valid JSON, breaks a field’s rules, or has a field this operation does not accept ("property <name> should not exist"). |
| 400 | [`bad_request`](https://hub.main-team.org/api/errors#bad_request) | The exam is open, but the student has no grade. Set one with `updateOrgStudent` or `updateStudent`, then try again. |
| 401 | [`unauthorized`](https://hub.main-team.org/api/errors#unauthorized) | The token is missing or malformed, is not signed with your account’s `apiSecret`, breaks the `iat` and `exp` rules, has expired or been revoked, or its account is not active. All of these answer the same. |
| 403 | [`forbidden`](https://hub.main-team.org/api/errors#forbidden) | The token is valid, but no role on your account allows `application/create` on the organization in the path, or a role denies it. |
| 404 | [`not_found`](https://hub.main-team.org/api/errors#not_found) | `organizationId` is not the `_id` of an organization. |
| 404 | [`not_found`](https://hub.main-team.org/api/errors#not_found) | No student of yours has this id: it matches nobody, or another account registered the student. |
| 404 | [`not_found`](https://hub.main-team.org/api/errors#not_found) | `examId` is not the `_id` of an exam in this organization. |
| 409 | [`conflict`](https://hub.main-team.org/api/errors#conflict) | The student has never signed in to this organization, so it holds no record of them yet. Send them a sign-in link (`createSigninLink`), and try again once they have used it. |
| 409 | [`conflict`](https://hub.main-team.org/api/errors#conflict) | The exam is not open: applications to it are switched off, its sitting date has passed, or its category is inactive. |
| 409 | [`conflict`](https://hub.main-team.org/api/errors#conflict) | The exam does not accept the student’s grade. The message names the grades it does accept, such as "Exam is not available for grade 8. It accepts grade 9, 10." |
| 409 | [`conflict`](https://hub.main-team.org/api/errors#conflict) | The exam is restricted to countries the student is not in. The message names the countries it is offered in. |
| 409 | [`conflict`](https://hub.main-team.org/api/errors#conflict) | The exam has no language set, so it is offered to no student. |
| 409 | [`conflict`](https://hub.main-team.org/api/errors#conflict) | For any other reason, `listAvailableExams` leaves the exam out for this student. |
| 409 | [`conflict`](https://hub.main-team.org/api/errors#conflict) | The student already holds another exam in the same category on the same sitting, and nobody can sit both. Move that application (`moveApplication`) instead. |
| 413 | [`payload_too_large`](https://hub.main-team.org/api/errors#payload_too_large) | The body is larger than 100 kB. |
| 415 | [`unsupported_media_type`](https://hub.main-team.org/api/errors#unsupported_media_type) | The body declares a charset that is not a UTF one (send UTF-8), or a `Content-Encoding` other than gzip, deflate or br. |
| 429 | [`too_many_requests`](https://hub.main-team.org/api/errors#too_many_requests) | Your account has made more than 100 requests to this operation in the current 60-second window. Wait the seconds in `Retry-After` before sending again. |
| 500 | [`internal_error`](https://hub.main-team.org/api/errors#internal_error) | Something failed on our side. Retry later, and quote `request_id` if it goes on. |

## Code samples

### curl

```bash
# $TOKEN: a short-lived token you minted with your API key and secret
curl -sS -X POST 'https://api.main-team.org/v1/<organizationId>/application' \
  -H "Authorization: Bearer $TOKEN" \
  -H 'Content-Type: application/json' \
  --data-binary @- <<'JSON'
{
  "examId": "6650a1b2c3d4e5f6a7b8c9e1",
  "studentId": "6650a1b2c3d4e5f6a7b8c9d0"
}
JSON
```

### Node.js

```js
const token = process.env.TOKEN; // a short-lived token you minted with your API key and secret

const res = await fetch('https://api.main-team.org/v1/<organizationId>/application', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${token}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "examId": "6650a1b2c3d4e5f6a7b8c9e1",
    "studentId": "6650a1b2c3d4e5f6a7b8c9d0"
  }),
});
const body = await res.json();
if (!res.ok) throw new Error(`${res.status} ${body.error.code}: ${body.error.message}`);
console.log(body.data);
```

### PHP

```php
<?php
$token = getenv('TOKEN'); // a short-lived token you minted with your API key and secret

$ch = curl_init('https://api.main-team.org/v1/<organizationId>/application');
curl_setopt_array($ch, [
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . $token,
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'examId' => '6650a1b2c3d4e5f6a7b8c9e1',
        'studentId' => '6650a1b2c3d4e5f6a7b8c9d0',
    ]),
    CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
if ($response === false) {
    throw new RuntimeException(curl_error($ch));
}
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$body = json_decode($response, true);
if ($status >= 400) {
    $error = $body['error'];
    throw new RuntimeException("$status {$error['code']}: {$error['message']}");
}
print_r($body['data']);
```
