---
title: Using the Kentico API externally
related:
  - https://docs.kentico.com/k12sp/integrating-3rd-party-systems/using-the-kentico-api-externally/displaying-kentico-content-in-external-applications.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

This page describes how to configure external ASP.NET applications so that they support the Kentico API. Using Kentico functionality externally allows you to create scenarios such as:

- Separate applications integrated with your main Kentico website
- Custom websites or applications that use Kentico as a content repository

Start Visual Studio and open your external application's project. You need to perform the following steps:

1. [Connect to the Kentico database](#connecting-to-the-kentico-database)
2. [Integrate the Kentico API libraries](#integrating-kentico-api-libraries)
3. [Initialize the Kentico application](#initializing-the-kentico-application)

> **Note:** **License requirements**
>
> The Kentico API performs license checks even when running in an external application. For applications that run under a different domain than your main Kentico site (web applications, WCF services, etc.), you need to have a valid [license](https://docs.kentico.com/k12sp/configuring-kentico/managing-sites/managing-site-licenses.md) for the given domain.
>
> Add the license via the administration interface of the connected Kentico application – the license is available through the shared database.

## Connecting to the Kentico database

To be able to access the Kentico database, you need to specify the appropriate connection string in your application's **web.config** or **app.config** file.

Add the connection string into the _configuration/connectionStrings_ section using an __ element named **CMSConnectionString**.

```html

<configuration>
  <connectionStrings>

    ...

    <add name="CMSConnectionString" connectionString="Persist Security Info=False;database=Kentico;server=myserver;user id=username;password=mypassword;Current Language=English;Connection Timeout=120;" />

  </connectionStrings>

```

> **Tip:** **Tip**: We recommend copying the exact connection string from the web.config file of your Kentico web project.

## Integrating Kentico API libraries

Before you can use Kentico functionality in your application, you need to add the libraries containing the required code:

1. Right-click your application's project in the Visual Studio **Solution Explorer** and select **Manage NuGet Packages**.
2. Search for the [**Kentico.Libraries**](https://www.nuget.org/packages/Kentico.Libraries) package.
3. Install the _Kentico.Libraries_ version that matches the version of the connected Kentico database.
4. (Optional) If you wish to use Kentico controls or web form API in external web applications, you need to install the **Kentico.Libraries.Web.UI** package.

> **Note:** **Maintaining Kentico.Libraries**
>
> If you [upgrade or hotfix](https://docs.kentico.com/k12sp/external-utilities/kentico-hotfix-and-upgrade-utility/upgrading-and-hotfixing-an-instance.md) the related Kentico project and its database to a newer version, you also need to update the Kentico NuGet packages to a matching version in your external application.

## Initializing the Kentico application

You need to initialize Kentico before making calls to its API from an external project.

**Web applications**

1. Edit your application's [Global.asax](http://msdn.microsoft.com/en-us/library/1xaas8a2%28v=vs.71%29.aspx) file (create the file if necessary).
2. Execute the **CMSApplication.Init** method in the **Application\_BeginRequest** method:

   ```csharp

   void Application_BeginRequest(object sender, EventArgs e)
   {
       CMS.DataEngine.CMSApplication.Init();
   }

   ```

If you wish to load and display content from the Kentico database in an external Web Forms application, you need to perform additional steps. See [Displaying Kentico content in external applications](https://docs.kentico.com/k12sp/integrating-3rd-party-systems/using-the-kentico-api-externally/displaying-kentico-content-in-external-applications.md).

**Non-web applications (for example Console applications)**

1. Edit your application's **Main** method.
2. Execute the **CMSApplication.Init** method at any point before the first call of the Kentico API.

You can now use the Kentico API in your external application. The Kentico API allows you to perform any action available in the standard administration interface.

> **Note:** **Using custom code that requires discovery**
>
> For external projects that compile into a different output type than a DLL assembly (for example Console applications), do NOT directly add any custom classes that need to be detected during the API initialization. This includes classes registered using attributes, such as [generated wrapper classes](https://docs.kentico.com/k12sp/developing-websites/generating-classes-for-kentico-objects.md) for objects, [custom module classes](https://docs.kentico.com/k12sp/custom-development/creating-custom-modules.md), custom implementations of interfaces or providers, etc. The system is only able to discover such code within assemblies.
>
> Instead, add the code into a _Class Library_ project with the _AssemblyDiscoverable_ attribute and reference the assembly from your project.

**WCF services**

If implementing custom WCF services within the Kentico web project, you need to initialize the Kentico application before calling the Kentico API within the service code.

Otherwise you may encounter errors if the first request that starts the application is destined for the WCF service. The standard initialization does not occur, because WCF requests are not processed by the ASP.NET HTTP runtime. For more information, see the [WCF Services and ASP.NET](https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-services-and-aspnet) article.

For example, you can execute the **CMSApplication.Init** method within the constructor of your WCF service class.

## Additional configuration and tips

### Sharing project files with the Kentico application

If you need to call API that works with a specific folder in the Kentico project, you can map the physical path used by the Kentico API in your external application to a specific Kentico project. For example, you can use this approach if you wish to externally update or rebuild the files of [locally stored search indexes](https://docs.kentico.com/k12sp/configuring-kentico/setting-up-search-on-your-website/using-locally-stored-search-indexes.md).

Set the **SystemContext.WebApplicationPhysicalPath** property to the path of your Kentico project's **CMS** folder. Map the path _before_ you initialize the Kentico API (i.e. call _CMSApplication.Init_), for example in the **Application\_Start** method of _Global.asax,_ or in your application's **Main** method.

```csharp title="Example"

// Maps the physical path used by the Kentico API in the external application to the folder of the connected Kentico project
CMS.Base.SystemContext.WebApplicationPhysicalPath = "C:\\inetpub\\wwwroot\\Kentico\\CMS";

```

### Working with the user context

When working with the Kentico API in an external application, the default Kentico user context is not available. The standard API for getting the current user (_**CMS.Membership.MembershipContext.AuthenticatedUser**_) always returns the "public" user when called externally.

To use API that relies on the context of a specific user, enclose the code into a **using** statement with a declared **CMSActionContext** instance and the appropriate _UserInfo_ object as a parameter.

```csharp title="Example"

using CMS.Base;
using CMS.Membership;
using CMS.EventLog;

...

// Gets an object representing a specific Kentico user
UserInfo user = UserInfoProvider.GetUserInfo("Andy");

// Sets the context of the user
using (new CMSActionContext(user))
{
    // Logs an information event into the Kentico event log, with "Andy" as the user who caused the event
    EventLogProvider.LogEvent(EventType.INFORMATION, "External Application", "Event_Code", "Details");
}

```

### Disabling the Kentico Virtual path provider

If you have the **Kentico.Libraries.Web.UI** package installed, Kentico uses a [virtual path provider](https://docs.microsoft.com/en-us/dotnet/api/system.web.hosting.virtualpathprovider) to load virtual objects, such as ASCX [transformations](https://docs.kentico.com/k12sp/developing-websites/loading-and-displaying-data-on-websites/writing-transformations.md) or [page layouts](https://docs.kentico.com/k12sp/developing-websites/developing-websites-using-portal-engine/editing-page-layouts.md), from the database. When using the Kentico API externally in web applications, you may wish to disable the virtual path provider for the following reasons:

- To avoid conflicts if your application has its own virtual path provider implementation
- If your external application does not use Kentico virtual objects at all (disabling the virtual path provider reduces overhead)

To disable the Kentico virtual path provider:

1. Edit your external application's **web.config** or **app.config** file.
2. Add the **** element under the __ section.
3. Add the **CMSUseVirtualPathProvider** key and set the value to _false._

```xml

<configuration>

...

    <appSettings>
        <add key="CMSUseVirtualPathProvider" value="false" />
    </appSettings>

...

</configuration>

```

> **Note:** **Note**: Without the virtual path provider, you cannot use ASCX transformations (or other Kentico virtual objects) to display data in external web applications. See [Displaying Kentico content in external applications](https://docs.kentico.com/k12sp/integrating-3rd-party-systems/using-the-kentico-api-externally/displaying-kentico-content-in-external-applications.md) for more information and workarounds.
