Integration Nylas v2 to Nylas v3

How I can rewrite this code in Nylas v2
let account = await Nylas.with(accessToken).account.get();
but it doesn’t work in Nylas v3

I updated Nylas, and changed code on
const NylasConfigs = {
apiKey: functions.config().keys.apiKey,
apiUri: ‘http://api.eu.nylas.com
}

const nylas = new Nylas(NylasConfigs)

I don’t understand how I can get grand id my user use Nylas SDK?

Here is my code use Nylas v2, but I don’t understand how I need rewrite on Nylas v2
export const getCalendars = functions
.region(functionRegion)
.runWith({ timeoutSeconds: 100, memory: ‘512MB’ })
.https.onCall(async (data, context) => {
try {
if (!context.auth) {
//no ath!!
throw new functions.https.HttpsError(‘unauthenticated’, ‘missing auth’);
}
const uid = context?.auth?.uid;

  const keyDoc = await admin.firestore().collection(Collection.NylasKeys).doc(uid).get();
  if (!keyDoc.exists) {
    throw new functions.https.HttpsError('unauthenticated', 'missing auth');
  }

  const accessToken = keyDoc.data()?.accessToken;
  let account = await Nylas.with(accessToken).account.get();
  //For some crazy reason the node api doesn't work for this???
  const accountDetails = await fetch(
    `https://api.nylas.com/a/${functions.config().keys.nylasClientId}/accounts/${account.accountId}`,
    {
      method: 'GET',
      headers: {
        Authorization: `Basic ${Buffer.from(`${functions.config().keys.nylasClientSecret}:`).toString('base64')}`,
      },
    },
  );
  const accountDetailsObject = await accountDetails.json();
  log(accountDetailsObject as string);
  if (accountDetailsObject.sync_state != 'running') {
    //Calendar not ready yet!
    return {
      ready: false,
    };
  }
  log('Account ready');
  const calendars = await Nylas.with(accessToken).calendars.list();
  log('Got cals');
  log(calendars as unknown as string);
  const formattedCals = calendars.map(cal => {
    return {
      title: cal.name,
      timezone: cal.timezone,
      id: cal.id,
      accountId: cal.accountId,
    };
  });

  return {
    ready: true,
    calendars: formattedCals,
  };
} catch (e) {
  functions.logger.error(e);
  throw e;
}

});

Hello @Kateryna This is our Quickstart guide for Calendars Calendar API Quickstart guide: read and create events | Nylas Docs It has code samples in all of our SDKs and Node is the default one :wink:

@Blag Can you explain me, how I can get data about user, if I have access_token. I can’t find this in document Nylas.

In Nylas v2 used this let account = await Nylas.with(accessToken).account.get(); but I cant use it for Nylas v3.

Also I have this API https://api.nylas.com/a/${functions.config().keys.nylasClientId}/accounts/${account.accountId}
which Api I need use in Nylas v3

@Kateryna There’s nothing on the SDKs regarding account info, but there’s a way to get it :wink:

1 Like

This is Ruby code, but might give you an idea :slight_smile:

get '/oauth/exchange' do
  code = params[:code]
  status 404 if code.nil?

  begin
    response = nylas.auth.exchange_code_for_token({
                       client_id: ENV['V3_CLIENT'],
                       client_secret: ENV['V3_SECRET'],
                       redirect_uri: 'http://localhost:4567/oauth/exchange',
                       code: code
                       })
  rescue StandardError
    status 500
  else
    print(response[:id_token]) # Grab until the period ^[^.]+ and base64 decode this to get user info
    response[:grant_id]
    response[:email]
    session[:grant_id] = response[:grant_id]
  end
end

@Blag Thanks :blush:
I used this API and get grand_is

curl --request GET \
  --url https://api.nylas.com/a/<NYLAS_CLIENT_ID/accounts/<ACCOUNT_ID> \
  --header 'Accept: application/json' \
  -u "<NYLAS_CLIENT_SECRET>:" \
  --header 'Content-Type: application/json'