Skip to main content

Authentication API

exchangeAccessCodeForAuthTokens​

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 { exchangeAccessCodeForAuthTokens } from "psn-api";

const authorization = await exchangeAccessCodeForAuthTokens(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/exchangeAccessCodeForAuthTokens.ts


exchangeNpssoForAccessCode​

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 { exchangeNpssoForAccessCode } from "psn-api";

const accessCode = await exchangeNpssoForAccessCode("<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 exchangeAccessCodeForAuthTokens().

Source​

authenticate/exchangeNpssoForAccessCode.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 exchangeNpssoForAccessCode(npsso);
const authorization = await exchangeAccessCodeForAuthTokens(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