Azure Functions Documentation

Configure Application Settings

Application settings are key-value pairs that are used to configure your Azure Functions app. They are available to your function code at runtime as environment variables. This is the recommended way to manage configuration for your functions, including connection strings, API keys, and other sensitive information.

Understanding Application Settings

Application settings are stored in the appsettings.json file during local development and are applied to the Azure Function app when deployed. In Azure, these settings are managed through the Azure portal, Azure CLI, or deployment pipelines.

Managing Settings in the Azure Portal

  1. Navigate to your Azure Functions app in the Azure portal.
  2. In the left-hand menu, under the "Settings" section, select Configuration.
  3. You will see two main tabs: Application settings and Connection strings.
  4. Application settings: Use this tab for general configuration values.
  5. Connection strings: Use this tab specifically for storing connection strings to other Azure services (like Azure Storage, Azure SQL Database, etc.). These are often pre-populated for you when you create your Function App.

Adding a New Application Setting

To add a new setting:

  1. Click the New application setting button.
  2. Enter the Name of your setting (e.g., MY_API_KEY).
  3. Enter the Value for your setting (e.g., abcdef123456).
  4. Click OK.
  5. Remember to click Save at the top of the Configuration blade to apply your changes.

Adding a New Connection String

To add a new connection string:

  1. Click the New connection string button.
  2. Enter the Name for your connection string (e.g., AzureWebJobsStorage).
  3. Select the Type of connection string (e.g., Azure Storage, SQL Azure).
  4. Enter the Value or select a resource from the dropdown to automatically populate the connection string.
  5. Click OK.
  6. Click Save at the top of the Configuration blade.
Important: Application settings and connection strings are sensitive. Avoid hardcoding them directly in your function code. Use environment variables for access.

Accessing Settings in Your Function Code

C#

You can access application settings using the IConfiguration service or by directly reading environment variables.


using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Company.Function
{
    public static class MyCSharpFunction
    {
        [Function("MyCSharpFunction")]
        public static void Run(
            [TimerTrigger("0 */5 * * * *")] TimerInfo timerInfo,
            FunctionContext context)
        {
            var logger = context.GetLogger();
            var config = context.GetConfiguration();

            string apiKey = config.GetValue<string>("MY_API_KEY");
            logger.LogInformation($"API Key: {apiKey}");

            // Accessing connection strings
            string storageConnectionString = config.GetConnectionString("AzureWebJobsStorage");
            logger.LogInformation($"Storage Connection String: {storageConnectionString.Substring(0, 10)}..."); // Log first few chars for safety
        }
    }
}
            

JavaScript/TypeScript

Access settings via the process.env object.


const { app } = require('@azure/functions');

app.timer('myTimer', {
    schedule: '0 */5 * * * *',
    handler: async (timerInfo) => {
        console.log('Timer trigger function executed!', new Date());

        const apiKey = process.env.MY_API_KEY;
        console.log(`API Key: ${apiKey}`);

        // Accessing connection strings
        const storageConnectionString = process.env.AzureWebJobsStorage;
        console.log(`Storage Connection String: ${storageConnectionString.substring(0, 10)}...`);
    }
});
            

Python

Access settings using os.environ.


import logging
import azure.functions as func
import os

def main(timerInfo: func.TimerRequest) -> None:
    logging.info('Python timer trigger function executed at %s', os.environ.get('WEBSITE_TIME'))

    api_key = os.environ.get('MY_API_KEY')
    logging.info(f"API Key: {api_key}")

    # Accessing connection strings
    storage_connection_string = os.environ.get('AzureWebJobsStorage')
    logging.info(f"Storage Connection String: {storage_connection_string[:10]}...")
            
Pro Tip: For secrets like API keys or database credentials, consider using Azure Key Vault and integrating it with your Azure Functions app. This provides enhanced security and management capabilities.

Deployment Slots

When using deployment slots, application settings and connection strings can be configured independently for each slot. This allows you to test settings in a staging slot before swapping to production. You can configure slot-specific settings in the Configuration blade by selecting the desired slot.

Local Development with local.settings.json

During local development, use the local.settings.json file to store your application settings and connection strings. These values are not deployed to Azure but are used by the Azure Functions Core Tools to emulate the Azure environment.


{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "MY_API_KEY": "local_dev_api_key_12345"
  },
  "ConnectionStrings": {
    "MyDatabaseConnectionString": "Server=.;Database=MyLocalDb;Trusted_Connection=True;"
  }
}
            
Warning: Ensure that local.settings.json is included in your .gitignore file to prevent sensitive information from being committed to source control.