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
- Navigate to your Azure Functions app in the Azure portal.
- In the left-hand menu, under the "Settings" section, select Configuration.
- You will see two main tabs: Application settings and Connection strings.
- Application settings: Use this tab for general configuration values.
- 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:
- Click the New application setting button.
- Enter the Name of your setting (e.g., MY_API_KEY).
- Enter the Value for your setting (e.g., abcdef123456).
- Click OK.
- 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:
- Click the New connection string button.
- Enter the Name for your connection string (e.g., AzureWebJobsStorage).
- Select the Type of connection string (e.g., Azure Storage, SQL Azure).
- Enter the Value or select a resource from the dropdown to automatically populate the connection string.
- Click OK.
- Click Save at the top of the Configuration blade.
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]}...")
            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;"
  }
}
            local.settings.json is included in your .gitignore file to prevent sensitive information from being committed to source control.