Ricevi email: rotte

Refresh Token

Scambia un refresh token con un nuovo access token e un nuovo refresh token. La vecchia coppia smette subito di funzionare.

POSThttps://g.sendcorex.com/v1/token/refresh

Questa rotta non richiede l'header Authorization. L'access token dura 1 ora e il refresh token 15 giorni.

Il refresh token è monouso. Salva il nuovo token e il refresh_token prima di fare qualsiasi altra cosa, perché la vecchia coppia non vale più appena questa chiamata riesce.

Parametri del body

  • refresh_tokenstringobbligatorio

    Il refresh token attuale (R.S.C.G.…).

Avvio rapido

import axios from 'axios';

const response = await axios.post(
  'https://g.sendcorex.com/v1/token/refresh',
  {
    refresh_token: "R.S.C.G.xxxxxxxx"
  },
);

console.log(response.data);

Risposta

{
  "type": "SUCCESS",
  "data": {
    "token": "S.C.G.xxxxxxxx",
    "refresh_token": "R.S.C.G.xxxxxxxx",
    "expires_at": "2026-10-10T09:33:00Z",
    "refresh_expires_at": "2026-10-25T08:33:00Z"
  }
}

Campi della risposta

  • data.token

    Il nuovo access token. Invialo nell'header Authorization.

  • data.refresh_token

    Il nuovo refresh token. Usalo una volta, al prossimo refresh.

  • data.expires_at

    Quando scade l'access token (UTC).

  • data.refresh_expires_at

    Quando scade il refresh token (UTC).

Refresh automatico

Esegui il refresh quando una richiesta fallisce con TOKEN_EXPIRED, e assicurati che ne giri uno solo alla volta: il refresh token si usa una volta.

Node.js

import axios from 'axios';

const api = axios.create({
  baseURL: 'https://g.sendcorex.com',
  validateStatus: () => true, // handle 4xx/5xx ourselves
});

let token = process.env.SENDCOREX_TOKEN;
let refreshToken = process.env.SENDCOREX_REFRESH_TOKEN;

async function refresh() {
  const res = await api.post('/v1/token/refresh', { refresh_token: refreshToken });
  if (res.status !== 200) throw new Error('refresh failed: ' + res.data?.code);
  token = res.data.data.token;
  refreshToken = res.data.data.refresh_token;
  // Persist both values (database / secret store): the old refresh token is dead.
  return res.data.data;
}

// One refresh at a time, because the refresh token is single-use.
let refreshing = null;

export async function callWithRefresh(commands) {
  const send = () => api.post('/v1', { commands }, { headers: { Authorization: token } });

  let res = await send();
  if (res.status === 401 && res.data?.code === 'TOKEN_EXPIRED') {
    refreshing ??= refresh().finally(() => (refreshing = null));
    await refreshing;
    res = await send();
  }
  if (res.status !== 200) throw new Error(res.data?.code ?? 'HTTP ' + res.status);
  return res.data.results;
}

Se il refresh fallisce con INVALID_REFRESH_TOKEN, la coppia non si può recuperare. Genera un nuovo token nella dashboard.

Errori

Gli errori a livello HTTP restituiscono type, code e message. Gli errori marcati CMD tornano dentro il risultato del comando, con HTTP 200.

  • 401INVALID_REFRESH_TOKEN

    Il refresh token non è valido, è scaduto, revocato o già usato. Genera un nuovo token nella dashboard.

  • 429RATE_LIMITED

    Più di 15 richieste/s o 15 in corso per la stessa IP + casella. Attendi i secondi indicati in Retry-After.

  • 503AUTH_UNAVAILABLE

    L'autenticazione è temporaneamente non disponibile. Riprova.