Authenticating to a ServiceNow instance with the ServiceNow SDK

  • Release version: Washingtondc
  • Updated October 9, 2024
  • 4 minutes to read
  • Authenticate to a ServiceNow instance and store user credentials for accessing the instance on your system with the ServiceNow SDK.

    You can use basic or OAuth 2.0 credentials to authenticate to an instance. To use OAuth 2.0 authentication, you must first configure the instance to support OAuth 2.0 authentication with the ServiceNow SDK by importing the required XML configuration.

    After authenticating to an instance with the ServiceNow SDK command-line interface (CLI), you can begin developing applications and installing them on your instance. A non-production instance should be used for application development.

    Authenticate to a ServiceNow instance using basic authentication with the ServiceNow SDK

    Use basic authentication to connect to a ServiceNow instance with the ServiceNow SDK.

    Before you begin

    Role required: admin

    About this task

    This procedure uses the ServiceNow SDK command-line interface (CLI). From a command-line tool, enter now-sdk --help to get information about the available commands and global options. To get additional information about a command and its parameters, enter the command and --help or -h. For example, now-sdk auth --help. For more information about the CLI, see ServiceNow SDK CLI.

    Procedure

    1. Create a local directory for your application.
    2. In Visual Studio Code, open the directory.
    3. From the application directory, open an integrated Terminal window.
    4. Specify the instance to authenticate to with the auth command.
      npx @servicenow/sdk auth --add <instance>
      For example:
      npx @servicenow/sdk auth --add https://myinstance.service-now.com
      Note:
      Using the npx command installs the ServiceNow SDK in your application directory instead of globally.
    5. Respond to the following series of prompts.
      Prompt Response
      Type of authentication to use Select basic.
      Alias for these credentials Enter an alias for your credentials and the instance.

      The alias can be used for authentication with the init, transform, dependencies, and install commands.

      The username to authenticate with the instance Enter your username for the instance.

      You must be assigned the admin role.

      The password to authenticate with the instance Enter your password.

    Result

    The alias and credentials are stored in the device keychain or credential manager on your system and are set as the default credentials.

    What to do next

    Create a scoped application or convert an existing application for use with the ServiceNow SDK. For more information, see Create an application with the ServiceNow SDK or Convert an application with the ServiceNow SDK.

    Configure a ServiceNow instance for OAuth 2.0 authentication with the ServiceNow SDK

    Import the configuration to use OAuth 2.0 authentication with the ServiceNow SDK to an instance.

    Before you begin

    The default System Administrator (admin) user must elevate to the privileged security_admin role to configure an instance for OAuth 2.0 authentication with the ServiceNow SDK. For more information, see Elevate to a privileged role.

    Role required: security_admin

    Procedure

    1. Navigate to the Configuring ServiceNow SDK for OAuth 2.0 authentication (SSO) article in the ServiceNow Community.
    2. Download the attached ZIP file and unzip its contents.
    3. Import the XML to your instance.
      1. From your instance, navigate to any list.
        Any list can be used because the XML file contains the destination table name for the records.
      2. Select and hold (or right-click) any column title and select Import XML.
      3. In the Import XML form, select Choose File and select one of the downloaded XML files.
      4. Select Upload.
      5. Repeat these steps for each XML file.
      For additional information, see Import data from XML.

    Result

    Developers can authenticate to the instance using OAuth 2.0 with the ServiceNow SDK.

    Authenticate to a ServiceNow instance using OAuth 2.0 with the ServiceNow SDK

    Use OAuth 2.0 authentication to connect to a ServiceNow instance with the ServiceNow SDK.

    Before you begin

    An administrator must configure the instance to support using OAuth 2.0 with the ServiceNow SDK. For more information, see Configure a ServiceNow instance for OAuth 2.0 authentication with the ServiceNow SDK.

    Role required: admin

    About this task

    This procedure uses the ServiceNow SDK command-line interface (CLI). From a command-line tool, enter now-sdk --help to get information about the available commands and global options. To get additional information about a command and its parameters, enter the command and --help or -h. For example, now-sdk auth --help. For more information about the CLI, see ServiceNow SDK CLI.

    Procedure

    1. Create a local directory for your application.
    2. In Visual Studio Code, open the directory.
    3. From the application directory, open an integrated Terminal window.
    4. Specify the instance to authenticate to with the auth command.
      npx @servicenow/sdk auth --add <instance>
      For example:
      npx @servicenow/sdk auth --add https://myinstance.service-now.com
      Note:
      Using the npx command installs the ServiceNow SDK in your application directory instead of globally.
    5. Respond to the following series of prompts.
      Prompt Response
      Type of authentication to use Select oauth.
      Alias for these credentials Enter an alias for your credentials and the instance.

      The alias can be used for authentication with the init, transform, dependencies, and install commands.

      The ServiceNow SDK opens a web browser to authenticate with the instance.
    6. Navigate to the web page that opens and log in to the instance if you aren't currently logged in.
    7. Select Accept to allow the ServiceNow SDK to connect to the instance.
      The page refreshes and includes an authentication code.
    8. Select Copy to copy the authentication code provided.
    9. In the command line, paste the authentication code.

    Result

    The alias and credentials are stored in the device keychain or credential manager on your system and are set as the default credentials.

    What to do next

    Create a scoped application or convert an existing application for use with the ServiceNow SDK. For more information, see Create an application with the ServiceNow SDK or Convert an application with the ServiceNow SDK.