To get Session Id using Apex in Salesforce, UserInfo.getSessionId() is used.
Sample Code:
String sessionId = UserInfo.getSessionId();
Note: For Apex code that is executed asynchronously, such as @future methods, Batch Apex jobs, or scheduled Apex jobs, getSessionId returns null.