What is a Temporal Client?
A Temporal Client allows you to communicate with the Temporal Service. Communication with a Temporal Service lets you perform actions such as starting Workflow Executions, sending Signals to Workflow Executions, sending Queries to Workflow Executions, getting the results of a Workflow Execution, and providing Activity Task Tokens. When Standalone Activities are supported and enabled, a Temporal Client can also start and manage Standalone Activities directly, without involving a Workflow.
Code examples
The following examples show how to connect a Temporal Client to a local development Temporal Service and start a Workflow Execution.
- Go
- Java
- .NET
- PHP
- Python
- Ruby
- Rust
- TypeScript
package main
import (
"context"
"log"
"documentation-samples-go/yourapp"
"go.temporal.io/sdk/client"
)
func main() {
// Create a Temporal Client to communicate with the Temporal Cluster.
// A Temporal Client is a heavyweight object that should be created just once per process.
temporalClient, err := client.Dial(client.Options{
HostPort: client.DefaultHostPort,
})
if err != nil {
log.Fatalln("Unable to create Temporal Client", err)
}
defer temporalClient.Close()
}
// Create an instance that connects to a Temporal Service running on the local
// machine, using the default port (7233)
WorkflowServiceStubs serviceStub = WorkflowServiceStubs.newLocalServiceStubs();
// Initialize the Temporal Client
// This application uses the Client to communicate with the local Temporal Service
WorkflowClient client = WorkflowClient.newInstance(serviceStub);
using System;
using System.Threading.Tasks;
using Temporalio.Client;
var client = await TemporalClient.ConnectAsync(new TemporalClientConnectOptions
{
TargetHost = "localhost:7233",
Namespace = "default",
});
use Temporal\Client\GRPC\ServiceClient;
use Temporal\Client\WorkflowClient;
$serviceClient = ServiceClient::create('localhost:7233');
$workflowClient = WorkflowClient::create($serviceClient);
// Use $workflowClient to work with Workflows ...
import asyncio
from temporalio.client import Client
async def main():
client = await Client.connect("localhost:7233")
result = await client.execute_workflow(
YourWorkflow.run,
"your name",
id="your-workflow-id",
task_queue="your-task-queue",
)
print(f"Result: {result}")
if __name__ == "__main__":
asyncio.run(main())
client = Temporalio::Client.connect('localhost:7233', 'default')
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let runtime = CoreRuntime::new_assume_tokio(
RuntimeOptions::builder()
.telemetry_options(TelemetryOptions::builder().build())
.build()?,
)?;
let (conn_opts, client_opts) =
ClientOptions::load_from_config(LoadClientConfigProfileOptions::default())?;
let connection = Connection::connect(conn_opts).await?;
let client = Client::new(connection, client_opts)?;
}
import { Connection, Client } from '@temporalio/client';
async function run() {
const connection = await Connection.connect();
// your code goes here
const client = new Client({ connection });
}
run().catch((err) => {
console.error(err);
process.exit(1);
});