| How To Install The Rest API
   The Collect! REST API allows you to connect to the core application and access functionality without
  needing the User Interface.
   Depending on your configuration, you can install the API Server with or without Apache.
   Apache is used to proxy multiple services and for SSL Certificates. If you are running the API server
  alone and internally, then you may not need Apache.
   When you are ready to install,  contact Comtech Systems support to obtain a copy of the files.
   Unpack the files into the Collect\bin folder. The end result should be Collect\bin\companion.
 Port Selection  These instructions will reference the default ports for each service. If you are only running the
  API server internally on your network, then the simplest solution is to open just the ports for
  the API Server and the Authorization Server.
   If you plan to use the  Dashboard,  Client, or Consumer portals, then we recommend using Apache with
  ports 80 and 443 open in your firewall.
   The default ports mentioned below will be:
 
    
      | Service | Port |  
      | HTTP | 80 |  
      | HTTPS | 443 |  
      | API Server | 4001 |  
      | Authorization Server | 40000 |  
      | Consumer Portal | 3005 |  
      | Dashboard Portal | 3003 |  
      | Client Portal | 3000 |   
 Node.Js Installation   
    Go to https://nodejs.org.In the Download for Windows (x64) section, download the latest LTS release.Once downloaded, install NodeJS using the default settings.  
 SQL Server Port Configuration  The Collect! API requires SQL Server listening on a static port. Typically, this will have already
  been setup for Collect! to run on your network. If so, these instructions are informational as
  a double check.
    
    Open SQL Server Configuration Manager.In the left menu, toggle SQL Server Network Configuration and select Protocols for {Server
        Name}.
 If you don't know which server you are running Collect! on, you can  find out by opening the
         Collect\bin\sqlconfig.ini file. If the file contains just the server name, without an instance
         name, then your {Server Name} is MSSQLSERVER. Right-click TCP/IP and select Properties.In the Protocol tab, change Enabled to YesIn the IP Addresses tab, scroll to the IPALL section at the bottom and change TCP Port to
        1433.
 If 1433 is already in use by another instance of SQL Server, then you can choose another port
         like 1434. Click OK.In the left menu, select SQL Server Services.Right click SQL Server {Server Name} and select Restart. 
 WARNING: This will kick everyone out of Collect!. Only restart SQL Server when nobody is actively
             using the system.  
 SQL Server Login Setup  We recommend creating a dedicated account for the API server to access the database.
    
    Open SQL Server Management Studio (SSMS).Connect to your Instance used for Collect!.In the left pane, navigate to Security, then  Logins.Create a new login.The Login Name can be any value. For this exercise, we will use API.Select the SQL Server Authentication radio option.Enter a password and make note of it for configuration below.Uncheck the Enforce Password Policy box.In the left pane, go to Server Roles and select sysadmin.Click OK to save the account.  
 Email Setup  This is optional. If you plan to use the Portals, you will need to setup  email for Consumer
  registration or Client multi-factor authentication.
   Please refer to the Help topics  How To Setup Sending Email In Collect! and
   How To Setup A Local Email Server Using Hmailserver for more information.
   The current email templates are located in the Collect\bin\companion\server\templates folder.
   To change the email templates:
    
    Navigate to the Collect\bin\companion\server\templates folder.Open the folder for the applicable service.Rename the template file that you want to use and remove the word "template" from the file
        name. For example: welcome.template.hbs would become welcome.hbsOpen the email file with a text editor.Update the HTML file as needed and save. 
 To use database fields, you can use the following syntax {{ table.field }} Examples: {{  debtor.de_number }} or {{ nx_user.us_name }}
  
 Domain Configuration  This is only a requirement if you plan to access the API externally. This includes setting up the
  portals for Consumers, Clients, or the Dashboard.
 
 Setting up external access requires either a static IP  address from your Internet Service
          Provider, or for you to keep an eye on your external IP address and update it as needed.   Please refer to the Help document  How to Install and Setup Apache for more information.
  
 Apache Configuration  Apache is used to proxy the domain requests. This allows you to communicate on HTTPS for all
  requests, without having to open extra ports to the internet.
   Please refer to the Help document  How to Install and Setup Apache for the instructions to
  install Apache. This section will go over the vhost configuration for the Authorization Server.
    C:\wamp64\bin\Apache\Apache2.4.#\conf\httpd.conf  Add defines for the domains.
 Define DOMAIN_NAME_API api.yourdomain.com
   C:\wamp64\bin\Apache\Apache2.4.#\conf\extras\httpd-vhosts.conf  Copy and paste the below into the bottom of the file.
 <VirtualHost *:80>
  ServerName ${DOMAIN_NAME_API}
  ServerAlias ${DOMAIN_NAME_API}
  DocumentRoot "${INSTALL_DIR}/www"
  <Directory "${INSTALL_DIR}/www/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
    AllowOverride All
    Require local
  </Directory>
  <Directory "${INSTALL_DIR}/www/.well-known/">
    Require all granted
  </Directory>
  RewriteEngine on
  RewriteCond %{HTTP:Upgrade} =websocket [NC]
  RewriteRule ^ wss://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=301]
  RewriteCond %{REQUEST_URI} '!/.well-known/acme-challenge/'
  RewriteCond %{HTTP:Upgrade} !=websocket [NC]
  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=301]
</VirtualHost>
   C:\wamp64\bin\Apache\Apache2.4.#\conf\extras\httpd-ssl.conf  Copy and paste the below into the bottom of the file.
 <VirtualHost _default_:443>
# General setup for the virtual host
DocumentRoot "${INSTALL_DIR}/www"
ServerName ${DOMAIN_NAME_API}:443
ServerAdmin ${ADMIN_EMAIL}
ErrorLog "${SRVROOT}/logs/error.log"
TransferLog "${SRVROOT}/logs/access.log"
Header set X-Frame-Options sameorigin
Header set X-Content-Type-Options "nosniff"
Header set X-Permitted-Cross-Domain-Policies "none"
Header set Referrer-Policy "no-referrer"
Header set Cross-Origin-Resource-Policy "same-origin"
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
# SSL Engine Switch:
# Enable/Disable SSL for this virtual host.
SSLEngine on
# Server Certificate:
SSLCertificateFile "${SSL_FILE_PATH}/${DOMAIN_NAME_API}-crt.pem"
# Server Private Key:
SSLCertificateKeyFile "${SSL_FILE_PATH}/${DOMAIN_NAME_API}-key.pem"
# Server Certificate Chain:
SSLCertificateChainFile "${SSL_FILE_PATH}/${DOMAIN_NAME_API}-chain.pem"
<FilesMatch "\.(cgi|shtml|phtml|php)$">
  SSLOptions +StdEnvVars
</FilesMatch>
<Directory "${SRVROOT}/cgi-bin">
  SSLOptions +StdEnvVars
</Directory>
# SSL Protocol Adjustments:
BrowserMatch "MSIE [2-5]" \
      nokeepalive ssl-unclean-shutdown \
      downgrade-1.0 force-response-1.0
# Per-Server Logging:
CustomLog "${SRVROOT}/logs/ssl_request.log" \
        "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
RewriteEngine on
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://localhost:40000/$1 [P,L]
ProxyPreserveHost On
ProxyPass / http://localhost:40000/
ProxyPassReverse / http://localhost:40000/
</VirtualHost>
   Final Steps   
    If Apache is already configured and running, then you need to comment out
        "Include conf/extra/httpd-ssl.conf" line in the httpd.conf file.Restart the Apache service.Setup SSL Certificates.Uncomment "Include conf/extra/httpd-ssl.conf" in the httpd.conf file.Restart Apache.  
 Collect! Config Folder  At present, the Collect! API needs to know which databases have been authorized for connections.
  This is done via the api-databases.txt file.
 
 In the future, we will be replacing the text file with the ENV files in the CLI\config
          folder. 
  Navigate to the Collect\config folder. Create an API folder, if one does not already exist.In the API folder, create a text file called: api-databases.txtOpen the file api-databases.txt with a text editor and add the databases that should be available
      in the API.cv_masterdb
cvr_masterdb
cvx_masterdb
Save the file. 
 Remember to include the Report and Import databases. 
 This file already exists for new Collect! installations. It it not included in a patch to
          avoid overwriting an existing configuration.  
 Collect! API Application  The cvapi.exe application is only available in the latest release of Collect!. As the API is a live
  development project, it is recommended to stay up-to-date with patches.
   The best option for running the API is via the Windows  Scheduler.
    
    Create a Windows Batch file in the Collect\bin folder called shutdowncollect.bat.Edit the file with a text editor.Add the following code:taskkill.exe /f /im cv13.exe
taskkill.exe /f /im cvapi.exe
Save and close the Batch file.In Collect! create a new  Operator with the highest privileges. Select the Password does not
        expire box.Open the Windows  Task Scheduler.Create a new Task for the Shutdown.
      
        Schedule the task that works best for you based on your environment and backups.For the action, select the shutdowncollect.bat file.Save the Task.Create a new Task for the API server.
      
        Schedule the task that works best for you based on your environment and backups.For the action, select cvapi.exe.In the Add Arguments box, put in: /m /S4001 /dbmasterdb /i{ID} /p{password} /minSave the Task. 
 ID and Password are from the Collect! Operator that you created. 
 We use port 4001, but you can use any port as long as you configure it properly below.  
 Powershell Setup  The remaining steps are to be done in PowerShell. Open PowerShell as an Administrator.
   If needed, run the below to set the Execution Policy.
 Set-ExecutionPolicy Bypass
  Any references below to {Collect} are to be replaced with your actual Collect! directory. For
  example: C:\Collect.
 
 If you are running Collect! on another drive, you will need to enter that drive first,
          then push enter. For example: D:{enter}  
 Cli Configuration  The CLI folder stores the database connection configuration. Each file has a ENV extension.
   The config.env file contains generic configuration information.
   Each database needs to have its own ENV file. For example: cv_masterdb.env
    You will need to contact us to obtain the latest installation package.
    Generic Configuration File   
    Unpack the installation package into the {Collect}\bin\companion\ folder. This should result
    in {Collect}\bin\companion\cli and {Collect}\bin\companion\server.Navigate to the CLI folder in Powershell Admin mode.cd {Collect}\bin\companion\cli
Run the Setup Script../setup.ps1
Create the Generic Configuration file.node ./main.js config new
     Enter the Collect! API host server. Typically, it's the same computer that Collect! is
        installed on. You can enter 127.0.0.1, localhost, or your server's name.Enter the Collect! API Port. Typically, it's 4001, but use the value that you configured
        for the Windows Scheduler above.    Database Configuration File   
    Create the Database Configuration file.node ./main.js config new:database
     Enter the Database Name. Default databases, like cv_masterdb will be selectable. Other ones
        will need to be typed in.Enter the SQL Server Login Name that you created above.Enter the SQL Server Login Password.Enter the Collect! SQL Server host. Typically, it's the same computer that Collect! is
        installed on. You can enter 127.0.0.1, localhost, or your server's name. If you use a
        different database server, enter the server's IP address or name.Enter the Collect! SQL Server Port. Typically, it's 1433, but use the value that you configured
        in the SQL Configuration Manager above.Select the Encryption option that is applicable to your environment. Typically, if you are
        running the Collect! application and database on the same server, encryption is not needed.Select (Y)es to test the connection.Enter the amount of time for inactive sessions before having to log back in. Default is 30
        minutes.Enter the amount of time before having to the refresh the tokens. Default is 1 hour.    Multiple Databases  If you plan to run multiple databases with access to the API, then you will need to specify a
  default database. If you do not specify a default database, cv_masterdb will be assumed.
    
    Open the Collect\bin\companion\cli\config\config.env file with a text editor.Add the following to the bottom of the file:DEFAULT_DATABASE = {database_name}
Repeat the steps above under "Database Configuration File" for each database. 
 Use the same SQL Server credentials for all databases for simplicity.  
 Collect! Authorization Server  The Authorization Server is the gateway for the API. All incoming requests go there, then they are
  routed to the Collect! API Server. This allows you to setup a proxy load balancer, if you wanted.
   The Authorization Server also manages authentication. When you connect with your Collect! credentials,
  the server returns a token that you will use with all future requests for the session.
    
    Navigate to the Server folder in Powershell Admin mode.cd {Collect}\bin\companion\server
Run the Setup Script../setup.ps1
   
 The Authorization Server currently only allows access to Collect!  User Levels that are
          set as Function Manager, Client, or Collect. Sign in to Collect! and set your User Levels
          appropriately.  
 Minio Storage  This is optional and may not be in the companion server package, if you don't need it. It provides
  storage capabilities by connecting to Amazon Web Services S3 storage services.
    
    Navigate to the MinIO folder.cd {Collect}\bin\companion\minio
Run the Setup Script../setup.ps1
    
 Troubleshooting  If you restart your server, the PM2 services may not be running. Run the resurrect code in
  PowerShell.
 pm2 resurrect
 
 | 
Was this page helpful? Do you have any comments on this document? Can we make it better? If so how may we improve this page.
Please click this link to send us your comments: helpinfo@collect.org