Hardhat's configuration system provides a flexible, type-safe way to manage project settings, paths, and runtime variables with support for environment-based configuration and validation.
Creates runtime configuration variables that can be resolved from different sources like environment variables, files, or user input.
/**
* Creates a configuration variable to be fetched at runtime
* @param name - The name of the configuration variable
* @param format - Format string with placeholder marker (default: just the marker)
* @returns ConfigurationVariable object
*/
function configVariable(
name: string,
format?: string
): ConfigurationVariable;
interface ConfigurationVariable {
_type: "ConfigurationVariable";
name: string;
format?: string;
}Usage Examples:
import { configVariable } from "hardhat/config";
// Simple configuration variable
const apiKey = configVariable("API_KEY");
// With custom format
const rpcUrl = configVariable("RPC_URL", "https://example.com/{}");Configuration variables that have been resolved with validation and type conversion methods.
interface ResolvedConfigurationVariable {
_type: "ResolvedConfigurationVariable";
format: string;
/** Returns the raw value of the configuration variable */
get(): Promise<string>;
/** Returns the value validated as a URL */
getUrl(): Promise<string>;
/** Returns the value interpreted as a BigInt */
getBigInt(): Promise<bigint>;
/** Returns the value as a valid hex string */
getHexString(): Promise<string>;
}
type ConfigurationVariableResolver = (
variableOrString: ConfigurationVariable | string
) => ResolvedConfigurationVariable;Interface for user-defined configuration in hardhat.config files.
interface HardhatUserConfig {
paths?: ProjectPathsUserConfig;
tasks?: TaskDefinition[];
}
interface ProjectPathsUserConfig {
cache?: string;
artifacts?: string;
tests?: string | TestPathsUserConfig;
sources?: string | string[] | SourcePathsUserConfig;
}
interface TestPathsUserConfig {}
interface SourcePathsUserConfig {}Usage Examples:
import { HardhatUserConfig } from "hardhat/config";
const config: HardhatUserConfig = {
paths: {
sources: "./contracts",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
}
};
export default config;The final resolved configuration with all paths made absolute and defaults applied.
interface HardhatConfig {
paths: ProjectPathsConfig;
tasks: TaskDefinition[];
}
interface ProjectPathsConfig {
/** Absolute path to project root directory */
root: string;
/** Absolute path to config file, if loaded from file */
config?: string;
/** Absolute path to cache directory */
cache: string;
/** Absolute path to artifacts directory */
artifacts: string;
/** Resolved test paths configuration */
tests: TestPathsConfig;
/** Resolved source paths configuration */
sources: SourcePathsConfig;
}
interface TestPathsConfig {}
interface SourcePathsConfig {}Type for handling sensitive data that can be provided as literals or configuration variables.
type SensitiveString = string | ConfigurationVariable;Functions for defining global command-line options that are available to all tasks.
/**
* Defines a global option with a value
*/
function globalOption<T extends ArgumentType>(options: {
name: string;
shortName?: string;
description: string;
type?: T;
defaultValue: ArgumentTypeToValueType<T>;
}): OptionDefinition;
/**
* Defines a global flag (boolean option)
*/
function globalFlag(options: {
name: string;
shortName?: string;
description: string;
}): OptionDefinition;
/**
* Defines a global level option (for logging/verbosity control)
*/
function globalLevel(options: {
name: string;
shortName?: string;
description: string;
}): OptionDefinition;Functions for programmatic creation of Hardhat Runtime Environment and configuration loading.
/**
* Imports user configuration from a config file
*/
function importUserConfig(configPath: string): Promise<HardhatUserConfig>;
/**
* Resolves the path to the Hardhat configuration file
*/
function resolveHardhatConfigPath(projectRoot?: string): string | undefined;
/**
* Creates a Hardhat Runtime Environment programmatically
*/
function createHardhatRuntimeEnvironment(
config: HardhatConfig,
userConfig: HardhatUserConfig,
globalOptions: GlobalOptions
): Promise<HardhatRuntimeEnvironment>;Usage Examples:
import { configVariable, type SensitiveString } from "hardhat/config";
// Using literal string (not recommended for production)
const privateKey: SensitiveString = "0x1234...";
// Using configuration variable (recommended)
const privateKey: SensitiveString = configVariable("PRIVATE_KEY");