> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploys & Git integration

> Inspect deploy history, link a repository through the Square Cloud GitHub App, or use the legacy webhook flow.

```go theme={null}
client := rest.NewClient(os.Getenv("SQUARECLOUD_API_KEY"))
defer client.Close()

api := rest.New(client)
```

## Listing the deploy history

`api.GetApplicationDeployments(appID)` returns `[][]squarecloud.Deployment` — **one inner slice per deploy**. Every event in a group shares the same `ID` (the commit SHA-1, 40 hex chars) and walks through the lifecycle states: `pending → clone → commit → restarting → success | error`.

```go theme={null}
history, err := api.GetApplicationDeployments("abc123def456abc123def456")
if err != nil {
	panic(err)
}

for _, timeline := range history {
	last := timeline[len(timeline)-1]
	fmt.Printf("Deploy %s ended as %s\n", last.ID, last.State)

	for _, event := range timeline {
		if event.State == "clone" {
			fmt.Println("  cloned branch", event.Branch)
		}
		if event.State == "commit" {
			fmt.Println("  files", event.Files.Added, event.Files.Removed, event.Files.Modified)
		}
	}
}
```

<Note>
  `Branch` is only populated on the `"clone"` event, and `Files` (`Added`, `Removed`, `Modified`) only on the `"commit"` event.
</Note>

## Inspecting the current configuration

`api.GetApplicationCurrentDeployment(appID)` returns a `squarecloud.DeploymentCurrent` with the GitHub App linkage currently configured.

```go theme={null}
current, err := api.GetApplicationCurrentDeployment("abc123def456abc123def456")
if err != nil {
	panic(err)
}

if current.App == nil {
	fmt.Println("no GitHub App linked")
} else {
	fmt.Println("linked to", current.App.FullName)
}
```

## Linking via the Square Cloud GitHub App (recommended)

`api.LinkApplicationGithubApp(appID, repoName, branch)` links a GitHub repository to the application via the official GitHub App.

```go theme={null}
err := api.LinkApplicationGithubApp("abc123def456abc123def456", "octocat/hello-world", "main")
if err != nil {
	panic(err)
}
```

To remove the link:

```go theme={null}
err := api.UnlinkApplicationGithubApp("abc123def456abc123def456")
```

<Warning>
  `LinkApplicationGithubApp` and `UnlinkApplicationGithubApp` require a **session token (JWT)** as your credential — plain API keys are not accepted by these endpoints. Pass it with `rest.WithToken(sessionToken)` or build the client with it.
</Warning>

## Linking via the legacy webhook flow

`api.PostApplicationDeployWebhook(appID, accessToken)` registers a GitHub Personal Access Token (`ghp_*` or `github_pat_*`) and returns the webhook URL to configure on GitHub.

```go theme={null}
webhookURL, err := api.PostApplicationDeployWebhook("abc123def456abc123def456", "ghp_xxx...")
if err != nil {
	panic(err)
}

fmt.Println("Configure GitHub to POST to:", webhookURL)
```

<Note>
  Pass the token `"@"` to remove the configured webhook.
</Note>

```go theme={null}
_, err := api.PostApplicationDeployWebhook("abc123def456abc123def456", "@")
```
