Skip to main content

Authentication API

exchangeCodeForAccessToken

This function lets you exchange your access code for access and refresh tokens. If you do not have an access code, see the guide on authenticating manually.

Example

import { exchangeCodeForAccessToken } from "psn-api";

const authorization = await exchangeCodeForAccessToken(accessCode);

Parameters

NameTypeDescription
accessCodestringYour access code.

Returns

An AuthTokenResponse object, containing the following properties:

NameTypeDescription
accessTokenstringUsed to retrieve data from the PSN API.
expiresInnumberWhen the access token will expire.
idTokenstring
refreshTokenstringUsed to retrieve a new access token after it expires with exchangeRefreshTokenForAuthTokens()
refreshTokenExpiresInnumberWhen the refresh token will expire.
scopestring
tokenTypestring

Source

authenticate/exchangeCodeForAccessToken.ts


exchangeNpssoForCode

This function lets you exchange your NPSSO token for an access code. If you do not have an NPSSO, see the guide on authenticating manually.

Example

import { exchangeNpssoForCode } from "psn-api";

const accessCode = await exchangeNpssoForCode("<my 64-digit NPSSO>");

console.log(accessCode); // --> "v3.ABCDEF"

Parameters

NameTypeDescription
npssoTokenstringYour NPSSO token.

Returns

Promise<string>

An access code, which can be exchanged for access and refresh tokens with exchangeCodeForAccessToken().

Source

authenticate/exchangeNpssoForCode.ts

exchangeRefreshTokenForAuthTokens

This function lets you exchange your refresh token retrieved from your initial log in for a set of new access and refresh tokens. By using this function, you no longer need to constantly retrieve a new NPSSO.

Example

import { exchangeRefreshTokenForAuthTokens } from "psn-api";

// Let's assume at some point in history
// we've done the below initial log in.
const accessCode = await exchangeNpssoForCode(npsso);
const authorization = await exchangeCodeForAccessToken(accessCode);

// Now, let's pretend `authorization.accessToken` has expired.
// Rather than using the above process (via NPSSO) to get a new one,
// we'll use our refresh token to get a new one:
const newAuthTokens = await exchangeRefreshTokenForAuthTokens(
authorization.refreshToken
);

Parameters

NameTypeDescription
refreshTokenstringA previously retrieved and non-expired refresh token.

Returns

An AuthTokenResponse object, containing the following properties:

NameTypeDescription
accessTokenstringUsed to retrieve data from the PSN API.
expiresInnumberWhen the access token will expire.
idTokenstring
refreshTokenstringUsed to retrieve a new access token after it expires.
refreshTokenExpiresInnumberWhen the refresh token will expire.
scopestring
tokenTypestring

Source

authenticate/exchangeRefreshTokenForAuthTokens.ts