Client Emitters
How to Use Emitters to Generate HTTP Clients from TypeSpec
Section titled âHow to Use Emitters to Generate HTTP Clients from TypeSpecâIntroduction
Section titled âIntroductionâThis guide will walk you through the process of using different client emitters (JavaScript, Python, Java, C#) to generate HTTP clients from TypeSpec. Please note that all client emitters are currently in preview and are subject to changes in future versions.
By following this guide, you will learn:
- How to set up client emitters in
package.json
. - Update the client emitter configurations in
tspconfig.yaml
. - How to generate HTTP clients for each specific programming language.
Location of All Client Emitters
Section titled âLocation of All Client EmittersâThe client emitters are defined in the package.json
file within your project.
Emitter Name | Language | Version |
---|---|---|
@typespec/http-client-js | JavaScript | |
@typespec/http-client-python | Python | |
@typespec/http-client-java | Java | |
@typespec/http-client-csharp | C# |
Below is an example of the package.json
snippet where client emitters are defined:
"dependencies": { "@typespec/http-client-csharp": "^0.1.9-alpha.20250113.2", "@typespec/http-client-java": "^0.1.9", "@typespec/http-client-python": "^0.6.6", "@typespec/http-client-js": "^0.38.1", }
Client Emitter Settings
Section titled âClient Emitter SettingsâThis part provides an overview of the common and language-specific settings for each client emitter. These settings are stored in the tspconfig.yaml
file.
Common Configuration Options
Section titled âCommon Configuration OptionsâThe below option applies to all client emitters.
emitter-output-dir
: Defines where the generated SDK files will be stored.
JavaScript Client Emitter Settings
Section titled âJavaScript Client Emitter SettingsâJavaScript generally requires minimal configuration. However, it is recommended to provide packageDetails
for package metadata, which is used in package.json
and README.md
files.
packageDetails
Section titled âpackageDetailsâProvide the metadata for package.json
, README.md
information.
Property | Description |
---|---|
name | Package name used in package.json |
description | Package description used in package.json file |
version | Detailed version for your package, the default value is 1.0.0-beta.1 |
Example configuration:
packageDetails: name: "${your_package_name}" version: 1.0.0
Java Client Emitter Settings
Section titled âJava Client Emitter SettingsâPrerequisites
Section titled âPrerequisitesâBefore using the Java client emitter, ensure the following dependencies are installed:
- Java 17 or later - Download here
(Verify installation withjava --version
) - Maven - Download here
(Verify installation withmvn --version
)
C# Client Emitter Settings
Section titled âC# Client Emitter SettingsâBefore using the C# client emitter, ensure that the .NET 8.0 SDK (or higher) is installed. Full configuration options can be found in the C# Client Emitter README
JavaScript Client Emitter Example
Section titled âJavaScript Client Emitter ExampleâStep 1: Install Dependencies
Section titled âStep 1: Install DependenciesâAdd the following dependencies to your package.json
file:
"dependencies": { "@typespec/http-client-js": "^0.38.1"}
Run the following command to install the dependencies:
tsp install
Step 2: Update Configuration
Section titled âStep 2: Update ConfigurationâUpdate your tspconfig.yaml
file with the following configuration:
emit: - "@typespec/http-client-js"options: "@typespec/http-client-js": emitter-output-dir: "{project-root}/clients/javascript" packageDetails: name: "your_package_name" version: 1.0.0
Step 3: Generate Client
Section titled âStep 3: Generate ClientâRun the following command to generate the JavaScript client:
tsp compile {path to main.tsp}/main.tsp
Python Client Emitter Example
Section titled âPython Client Emitter ExampleâStep 1: Install Dependencies
Section titled âStep 1: Install DependenciesâAdd the following dependencies to your package.json
file:
"dependencies": { "@typespec/http-client-python": "^0.6.6"}
Run the following command to install the dependencies:
tsp install
Step 2: Update Configuration
Section titled âStep 2: Update ConfigurationâUpdate your tspconfig.yaml
file with the following configuration:
emit: - "@typespec/http-client-python"options: "@typespec/http-client-python": emitter-output-dir: "{project-root}/clients/python"
Step 3: Generate Client
Section titled âStep 3: Generate ClientâRun the following command to generate the Python client:
tsp compile {path to main.tsp}/main.tsp
Java Client Emitter Example
Section titled âJava Client Emitter ExampleâStep 1: Install Dependencies
Section titled âStep 1: Install DependenciesâAdd the following dependencies to your package.json
file:
"dependencies": { "@typespec/http-client-java": "^0.1.9"}
Run the following command to install the dependencies:
tsp install
Step 2: Update Configuration
Section titled âStep 2: Update ConfigurationâUpdate your tspconfig.yaml
file with the following configuration:
emit: - "@typespec/http-client-java"options: "@typespec/http-client-java": emitter-output-dir: "{project-root}/clients/java"
Step 3: Generate Client
Section titled âStep 3: Generate ClientâRun the following command to generate the Java client:
tsp compile {path to main.tsp}/main.tsp
C# Client Emitter Example
Section titled âC# Client Emitter ExampleâStep 1: Install Dependencies
Section titled âStep 1: Install DependenciesâAdd the following dependencies to your package.json
file:
"dependencies": { "@typespec/http-client-csharp": "^0.1.9-alpha.20250113.2"}
Run the following command to install the dependencies:
tsp install
Step 2: Update Configuration
Section titled âStep 2: Update ConfigurationâUpdate your tspconfig.yaml
file with the following configuration:
emit: - "@typespec/http-client-csharp"options: "@typespec/http-client-csharp": emitter-output-dir: "{project-root}/clients/dotnet"
Step 3: Generate Client
Section titled âStep 3: Generate ClientâRun the following command to generate the C# client:
tsp compile {path to main.tsp}/main.tsp
Running Language-Specific Emitters in CLI
Section titled âRunning Language-Specific Emitters in CLIâ-
Ensure that your package.json file is correctly configured to include the necessary dependencies for running the emitters
-
Update the tspconfig.yaml file for properly configured for the language-specific emitter.
emit:- "@typespec/http-client-csharp"- "@typespec/http-client-java"- "@typespec/http-client-python"- "@typespec/http-client-js"options:"@typespec/http-client-csharp":emitter-output-dir: "{project-root}/clients/dotnet""@typespec/http-client-java":emitter-output-dir: "{project-root}/clients/java""@typespec/http-client-python":emitter-output-dir: "{project-root}/clients/python""@typespec/http-client-js":emitter-output-dir: "{project-root}/clients/javascript" -
Once the package.json and tspconfig.yaml files are updated, you need to install all required dependencies by running the following command in the project root:
Terminal window tsp install -
Run the emitter to compile your TypeScript code into the desired language. Use the following command to trigger the emitter and compile your project:
Terminal window tsp compile {path to main.tsp}/main.tsp