Guides
Reference data
Four read-only directories supply the ids the rest of the API asks for:
| Directory | What you use it for | Operations |
|---|---|---|
| Countries | The country field when you register a student | List countries, Get a country |
| Subdivisions | The subdivision field (a US state) when the student's country needs one | List a country's subdivisions |
| Grades | The grade field at registration, and the grade an exam is restricted to | List grades, Get a grade |
| Organizations | The <organizationId> in every /v1/<organizationId>/… path | List organizations, Get an organization |
The API never creates, changes or deletes a country, subdivision or grade. Each of these routes is a plain read, and a value that matches nothing is refused (see How reference fields resolve). Cities and schools are the one exception: a name that is not in our list yet is added automatically (see Cities and schools).
Permissions
All seven routes are flat routes: they have no <organizationId> in the path, so they act on the main record (mto). A role that grants one needs target mto or *.
| Route | Permission |
|---|---|
GET /v1/country, GET /v1/country/<id>, GET /v1/country/<id>/subdivision | country/read |
GET /v1/grade, GET /v1/grade/<id> | grade/read |
GET /v1/organization, GET /v1/organization/<id> | organization/read |
Without the permission the answer is 403 with code forbidden and the message
Insufficient role permissions. The Permissions page explains how roles match.
Countries
List countries
curl "https://api.main-team.org/v1/country?page=1&limit=100" \
-H "Authorization: Bearer $TOKEN"
{
"success": true,
"message": "Countries fetched successfully.",
"data": [
{
"_id": "630e0182c53dc79a6836e67e",
"name": "GERMANY",
"iso2": "DE",
"iso3": "DEU",
"createdAt": "2022-08-30T12:14:26.118Z",
"updatedAt": "2022-08-30T12:14:26.118Z"
},
{
"_id": "630e0182c53dc79a6836e67f",
"name": "ALGERIA",
"iso2": "DZ",
"iso3": "DZA",
"createdAt": "2022-08-30T12:14:26.118Z",
"updatedAt": "2022-08-30T12:14:26.118Z"
}
],
"pagination": { "page": 1, "limit": 100, "total": 246, "totalPages": 3 }
}
The list is paginated like every listing: page defaults to 1, and limit defaults to 20 with a
maximum of 100. A larger limit is lowered to 100 rather than refused (see Pagination).
The list has no guaranteed order, so page through all of it and index it yourself.
| Field | Type | Notes |
|---|---|---|
_id | string, 24 hex characters | The value country takes at registration. |
name | string | Stored in upper case, for example "GERMANY". |
iso2 | string | Two-letter code, upper case. It is also the first two characters of every username minted for a student in that country. |
iso3 | string | Three-letter code, upper case. |
tz, dialCode, flag | string | Present on some countries. tz is the IANA time zone a sitting on local time is read in for students of the country, unless the student has a subdivision, whose own tz then decides. |
subdivisionRequired | boolean | true when students of the country must have a subdivision (the United States). Absent or false otherwise. |
subdivisionLabel | string | What the country calls its subdivisions, for your own forms (State). Absent when it has none. |
createdAt, updatedAt | ISO 8601 timestamp |
A country may carry other fields. Ignore fields you do not use.
Note
Some countries cannot be selected. They are left out of the list and out of the total, and
GET /v1/country/<id> answers for one of them exactly as it answers for an id that does not exist.
A registration or update that names one is refused with 400 and country is not a known country.
Get one country
curl "https://api.main-team.org/v1/country/630e0182c53dc79a6836e67e" \
-H "Authorization: Bearer $TOKEN"
{
"success": true,
"message": "Country fetched successfully.",
"data": {
"_id": "630e0182c53dc79a6836e67e",
"name": "GERMANY",
"iso2": "DE",
"iso3": "DEU",
"createdAt": "2022-08-30T12:14:26.118Z",
"updatedAt": "2022-08-30T12:14:26.118Z"
}
}
Country ids are the same everywhere
A country has the same _id on every olympiad, and an exam's countries are ids from this same list. To be certain, compare by iso2.
Why country takes an id and nothing else
At registration grade, city and school accept a name, but country accepts only the _id from
this list. A country has three common spellings (its name, its ISO2 code and its ISO3 code), and
choosing between them is ambiguity the field does not need. The country also scopes how a city or
school name is looked up, so it has to be exact. Build a lookup from iso2 (or whatever code your
own records use) to _id once, and translate on your side.
Subdivisions
A subdivision is a part of a country treated like a country for exam time and exam targeting. Today it is the 50 United States and the District of Columbia; territories stay
separate countries. A country whose subdivisionRequired is true needs every student
to have one.
List a country's subdivisions
curl "https://api.main-team.org/v1/country/630e0182c53dc79a6836e67e/subdivision?limit=100" \
-H "Authorization: Bearer $TOKEN"
{
"success": true,
"message": "Subdivisions fetched successfully.",
"data": [
{
"_id": "6650a1b2c3d4e5f6a7b8c9f2",
"country": "630e0182c53dc79a6836e67e",
"code": "US-CA",
"name": "CALIFORNIA",
"tz": "America/Los_Angeles",
"order": 5
}
],
"pagination": { "page": 1, "limit": 100, "total": 51, "totalPages": 1 }
}
The ids shown are made up. The list is in the order the platform's own forms use, hidden subdivisions
are left out, and a country without subdivisions answers an empty page. An unknown country, or one that
cannot be selected, answers 404 exactly as Get one country does. The list rarely
changes: fetch it once and keep it.
| Field | Type | Notes |
|---|---|---|
_id | string, 24 hex characters | The value subdivision takes. The same on every olympiad. |
country | string | The _id of the country it belongs to. |
code | string | The ISO 3166-2 code, upper case. subdivision takes it as well as the _id, in full: US-CA, never CA. |
name | string | Stored in upper case, for example "CALIFORNIA". |
tz | string | The IANA time zone students of the subdivision sit a local exam in. One main zone per state. |
order | number | The position in the platform's own forms. |
A state decides the time of a local sitting
An exam on local time starts at the same clock time wherever the student is. For a student with a
subdivision that is the subdivision's tz; without one it is the country's. This API does no time
arithmetic, so you do not compute it: it matters for what a student sees and for
state-targeted exams.
Grades
List grades
curl "https://api.main-team.org/v1/grade?limit=100" \
-H "Authorization: Bearer $TOKEN"
{
"success": true,
"message": "Grades fetched successfully.",
"data": [
{
"_id": "630e01826836e67ec53dc79d",
"name": "1",
"createdAt": "2022-08-30T12:14:26.301Z",
"updatedAt": "2022-08-30T12:14:26.301Z"
},
{
"_id": "630e01826836e67ec53dc79e",
"name": "2",
"createdAt": "2022-08-30T12:14:26.302Z",
"updatedAt": "2022-08-30T12:14:26.302Z"
}
],
"pagination": { "page": 1, "limit": 100, "total": 12, "totalPages": 1 }
}
Grades come back in the order they were created, oldest first. They are not sorted by name: names
are strings, and sorting them as text would put "10" before "2".
| Field | Type | Notes |
|---|---|---|
_id | string, 24 hex characters | The same id on every organization. |
name | string | The grade as a school would say it, for example "10". |
createdAt, updatedAt | ISO 8601 timestamp |
Grade ids are the same everywhere
A grade has the same _id on every organization. That is why the API is strict about the grade field: a grade that does not exist in this list is a
student no exam will ever accept.
A name works too
When you register or update a student, grade takes either an _id from this list or its name.
"grade": "10" is resolved to the grade whose name is 10, and that grade's _id is what gets stored,
so the two forms end in the same place. A name that matches no grade is refused with 400 and
grade is not a known grade. This API does not create reference data. This list shows which names are
accepted.
Graduated is not a grade you can set
The platform has one grade after "12": GRADUATED ("Graduated"), for a student who has left school. No
exam is for it. listGrades leaves it out, and registerStudent, checkStudentRegistration, each row of
createStudentImport, updateStudent and updateOrgStudent refuse it, by name or by _id, with 400 and
grade cannot be Graduated: … (in a check, a problems entry on grade; in an import, the row code
unknown_reference). A student who graduated on the platform carries it: the reads return it, getGrade
answers its _id, and an update that sends back the grade the student already holds is accepted.
Get one grade
curl "https://api.main-team.org/v1/grade/630e01826836e67ec53dc79d" \
-H "Authorization: Bearer $TOKEN"
{
"success": true,
"message": "Grade fetched successfully.",
"data": {
"_id": "630e01826836e67ec53dc79d",
"name": "1",
"createdAt": "2022-08-30T12:14:26.301Z",
"updatedAt": "2022-08-30T12:14:26.301Z"
}
}
Organizations
List organizations
curl "https://api.main-team.org/v1/organization" \
-H "Authorization: Bearer $TOKEN"
{
"success": true,
"message": "Organizations fetched successfully.",
"data": [
{
"_id": "64b7f0c2a1e4d93b5c2f1a02",
"name": "STEM Olympiad",
"slug": "stem",
"logo": "https://cdn.example.org/logos/stem.svg",
"desc": "International STEM olympiad.",
"defaultRedirect": "https://my.stemolympiad.org"
},
{
"_id": "64b7f0c2a1e4d93b5c2f1a03",
"name": "Hi-Lingua",
"slug": "hilingua",
"logo": "https://cdn.example.org/logos/hilingua.svg",
"desc": "International language olympiad."
}
],
"pagination": { "page": 1, "limit": 20, "total": 5, "totalPages": 1 }
}
The list holds the five organizations that run exams: stem, hilingua, neo, gmath and coding. mto,
the main record, is not listed. The routes without an <organizationId> act on it, so you never
need its id, and GET /v1/organization/<id> answers 404 for it.
An organization comes back with exactly these fields:
| Field | Type | Notes |
|---|---|---|
_id | string, 24 hex characters | The <organizationId> every /v1/<organizationId>/… route takes. |
name | string | Display name. |
slug | string | Short name: stem, hilingua, neo, gmath or coding. Used in role targets and in a student's activatedPlatformsThisSeason, never in a path. |
logo | string | Logo URL, when set. |
desc | string | Description, when set. |
defaultRedirect | string | Where a sign-in link sends the student when you give no redirect, when set. |
Use the _id, never the slug
/v1/stem/exam does not work. The path segment must be the organization's _id:
/v1/64b7f0c2a1e4d93b5c2f1a02/exam. A segment that is not 24 hex characters, or that names no
organization, is answered with 404, code not_found and the message
Organization not found!. That check runs after your token is checked, so a request without a valid
token gets 401 whatever the path says.
{
"error": {
"code": "not_found",
"message": "Organization not found!",
"documentation_url": "https://hub.main-team.org/api/errors#not_found",
"request_id": "3f9d2c1e-7a4b-4e0f-9c3d-1b2a5e6f7d80"
}
}
Organization ids do not change, so fetch the list once, keep a map from slug to _id, and use the
map everywhere you build a path. Organizations explains what the main record
and the five olympiads are, and why the flat routes act on mto.
Get one organization
curl "https://api.main-team.org/v1/organization/64b7f0c2a1e4d93b5c2f1a02" \
-H "Authorization: Bearer $TOKEN"
{
"success": true,
"message": "Organization fetched successfully.",
"data": {
"_id": "64b7f0c2a1e4d93b5c2f1a02",
"name": "STEM Olympiad",
"slug": "stem",
"logo": "https://cdn.example.org/logos/stem.svg",
"desc": "International STEM olympiad.",
"defaultRedirect": "https://my.stemolympiad.org"
}
}
Ids that are unknown or malformed
| You send | GET /v1/country/<id> | GET /v1/grade/<id> | GET /v1/organization/<id> |
|---|---|---|---|
| A well-formed id that matches nothing | 404, Country not found! | 404, Grade not found! | 404, Organization not found! |
| Anything that is not 24 hex characters | 400, Invalid value for '_id': expected ObjectId. | the same | the same |
A country that cannot be selected answers like an unknown id, and so does an organization the list
leaves out, mto included. Every 404 here has the code not_found, and
every 400 the code bad_request.
Cities and schools
There is no route that lists cities or schools. At registration you send their names instead.
If a school or city name is not in our list yet, it is added automatically when you register or update a student, or import students. You do not need to ask us first.
- Check first.
checkStudentRegistrationanswerswillCreate: { city, school },truefor a name that is not in our list yet. It adds nothing. A misspelt name becomes a new school, so look at the spelling wheneverwillCreateistrue. - Limits for a new name. A school name may be at most 120 characters and a city name 80, without
<or>. A longer or unclean name is refused with400(unknown_referencein an import). - An
_idthat matches nothing is refused, as is any unknowncountryorgrade: those are never added.
Caching
Reference data changes rarely, and each read counts against your rate limit (100 requests per 60 seconds per operation). A sensible pattern:
- Load all organizations, countries and grades when your integration starts, and keep them in memory or in your own store.
- Refresh them once a day.
- Refresh early when a student write is refused with
country is not a known country.orgrade is not a known grade.for an id you took from your cache. Then retry once with the fresh data.
This loader builds the three maps you need. It follows totalPages, so it works however large a
list grows.
const BASE = 'https://api.main-team.org/v1';
async function getAll(path, token) {
const items = [];
for (let page = 1; ; page++) {
const res = await fetch(`${BASE}${path}?page=${page}&limit=100`, {
headers: { Authorization: `Bearer ${token}` },
});
if (!res.ok) throw new Error(`${path}: HTTP ${res.status}`);
const body = await res.json();
items.push(...body.data);
if (page >= body.pagination.totalPages) return items;
}
}
export async function loadReferenceData(token) {
const [organizations, countries, grades] = await Promise.all([
getAll('/organization', token),
getAll('/country', token),
getAll('/grade', token),
]);
return {
organizationIdBySlug: new Map(organizations.map((o) => [o.slug, o._id])),
countryIdByIso2: new Map(countries.map((c) => [c.iso2, c._id])),
gradeIdByName: new Map(grades.map((g) => [g.name, g._id])),
};
}
// const ref = await loadReferenceData(token);
// ref.organizationIdBySlug.get('stem') // "64b7f0c2a1e4d93b5c2f1a02"
// ref.countryIdByIso2.get('AL') // "630e0182c53dc79a6836e67e"
<?php
const MT_BASE = 'https://api.main-team.org/v1';
function mt_get_all(string $path, string $token): array
{
$items = [];
for ($page = 1; ; $page++) {
$ch = curl_init(MT_BASE . $path . '?page=' . $page . '&limit=100');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $token],
CURLOPT_TIMEOUT => 30,
]);
$raw = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($raw === false || $status !== 200) {
throw new RuntimeException("$path: HTTP $status");
}
$body = json_decode($raw, true);
array_push($items, ...$body['data']);
if ($page >= $body['pagination']['totalPages']) {
return $items;
}
}
}
function mt_load_reference_data(string $token): array
{
$organizations = mt_get_all('/organization', $token);
$countries = mt_get_all('/country', $token);
$grades = mt_get_all('/grade', $token);
return [
'organizationIdBySlug' => array_column($organizations, '_id', 'slug'),
'countryIdByIso2' => array_column($countries, '_id', 'iso2'),
'gradeIdByName' => array_column($grades, '_id', 'name'),
];
}
// $ref = mt_load_reference_data($token);
// $ref['organizationIdBySlug']['stem']; // "64b7f0c2a1e4d93b5c2f1a02"
When a list is empty, totalPages is 0. Both loops stop after the first page either way.
Related
- Students: how
country,grade,cityandschoolare resolved when you register or update a student. - Organizations: the main record, the five olympiads and
<organizationId>. - Pagination and Rate limits.