Rise In Logo




Build on-chain with Circle and USDC

What Are Session Token And Encryption Key?

A Session Token is a temporary credential that is generated by the Circle API for authenticating the user for subsequent requests to the API endpoints. A session token expires after a certain period of time or when the user logs out.

The Encryption Key is an encryption and decryption key that is randomly generated to ensure the security of the session.

Let's look at the code.

Acquiring Session Token Javascript Code

import axios from "axios";

export const acquire_session_token = async () => {

  const options = {

    method: "POST",

    url: "<https://api.circle.com/v1/w3s/users/token>",

    headers: {

      "Content-Type": "application/json",

      Authorization: `Bearer ${process.env.NEXT_PUBLIC_API_KEY}`,

    },

    data: { userId: [process.env.NEXT](http://process.env.NEXT)\_PUBLIC_USER_ID },

  };

  return axios

    .request(options)

    .then(function (response) {

      console.log("user token:", [response.data.data](http://response.data.data).userToken);

      console.log("encryption key:", [response.data.data](http://response.data.data).encryptionKey);

      return {

        userToken: [response.data.data](http://response.data.data).userToken,

        encryptionKey: [response.data.data](http://response.data.data).encryptionKey,

      };

    })

    .catch(function (error) {

      console.error(error);

    });

};

Here the code starts with an axios import like the previous ones.

import axios from "axios";

Let's continue with the options object.

const options = {

  method: "POST",

  url: "<https://api.circle.com/v1/w3s/users/token>",

  headers: {

    "Content-Type": "application/json",

    Authorization: `Bearer ${process.env.NEXT_PUBLIC_API_KEY}`,

  },

  data: { userId: [process.env.NEXT](http://process.env.NEXT)\_PUBLIC_USER_ID },

};
  • method: "POST": This specifies that the HTTP method to be used is POST. POST is used to send data to a server to create/update a resource.
  • url: "https://api.circle.com/v1/w3s/users/token": This is the URL where the HTTP request will be sent.
  • headers: This is an object that holds HTTP headers that will be sent along with the request. In this case, two headers are being sent:
  • "Content-Type": "application/json": This tells the server that the data being sent is in JSON format.
  • Authorization: Bearer ${process.env.NEXT_PUBLIC_API_KEY}: This is used for authorization. It sends a bearer token, which is stored in the environment variable NEXT_PUBLIC_API_KEY, to the server to authenticate the request.
  • data: { userId: process.env.NEXT_PUBLIC_USER_ID }: This is the data that will be sent to the server in the body of the POST request. It's an object with a single property userId, the value of which is taken from the environment variable NEXT_PUBLIC_USER_ID.
return axios

  .request(options)

  .then(function (response) {

    console.log("user token:", [response.data.data](http://response.data.data).userToken);

    console.log("encryption key:", [response.data.data](http://response.data.data).encryptionKey);

    return {

      userToken: [response.data.data](http://response.data.data).userToken,

      encryptionKey: [response.data.data](http://response.data.data).encryptionKey,

    };

  })

  .catch(function (error) {

    console.error(error);

  });
  • axios.request(options): This sends an HTTP request using the options object defined earlier. The options object includes details like the HTTP method to use (POST), the URL to send the request to, the headers, and the data to send.
  • .then(function (response) {...}): This is a Promise that gets resolved when the HTTP request is successful. The response object contains the server's response to our request. Inside this function:
  • return { userToken: response.data.data.userToken, encryptionKey: response.data.data.encryptionKey, }; returns an object with userToken and encryptionKey from the response data.
  • .catch(function (error) {...}): This is another Promise that gets resolved if the HTTP request fails for any reason. The error object contains details about the error. Inside this function, console.error(error); logs the error to the console.

Now that you have a user(session) token and an encryption key, you are ready to initialize the user.

Summary

In summary, in this section:

  • You have retrieved a user token
  • You have retrieved an encryption key

Comments

Anonymous

0/500

You need to enroll in the course to be able to comment!

Stay in the know

Never miss updates on new programs and opportunities.

Rise In Logo

Rise together in web3!