# How to Use Invite Links Programmatically with wacli to Join WhatsApp Groups

> Learn to programmatically join WhatsApp groups using wacli invite links. Execute commands or call Go functions to automate group joining efficiently.

- Repository: [Peter Steinberger/wacli](https://github.com/steipete/wacli)
- Tags: how-to-guide
- Published: 2026-04-17

---

**Use `wacli groups join --code <CODE>` from the command line, or call `JoinGroupWithLink(ctx, code)` from the Go API in [`internal/wa/groups.go`](https://github.com/steipete/wacli/blob/main/internal/wa/groups.go) to programmatically join WhatsApp groups using invite links.**

The `wacli` project by steipete provides a command-line interface and Go library for automating WhatsApp operations. When you need to **use invite links programmatically with wacli to join groups**, the tool exposes direct methods that wrap the underlying WhatsMeow library. This guide covers both the CLI commands and the Go API implementation based on the actual source code.

## Core Methods for Invite Link Operations

The primary functionality resides in [`internal/wa/groups.go`](https://github.com/steipete/wacli/blob/main/internal/wa/groups.go), which exposes two core operations for managing invite links programmatically.

### GetGroupInviteLink

This method retrieves the current invite link for a group you administer. It can optionally force a reset to generate a new link and invalidate the previous one.

```go
// internal/wa/groups.go lines 65-73
func (c *Client) GetGroupInviteLink(ctx context.Context, group types.JID, reset bool) (string, error) {
    // Implementation forwards to WhatsMeow
}

```

### JoinGroupWithLink

This method consumes an invite code—the string segment after the final `/` in a WhatsApp group invite URL—and joins the authenticated client to that group.

```go
// internal/wa/groups.go lines 75-82
func (c *Client) JoinGroupWithLink(ctx context.Context, code string) (types.JID, error) {
    // Implementation forwards to WhatsMeow
}

```

## Using the CLI to Join Groups with Invite Codes

For command-line automation, `wacli` provides the `groups join` sub-command implemented in [`cmd/wacli/groups_invite_join.go`](https://github.com/steipete/wacli/blob/main/cmd/wacli/groups_invite_join.go).

### Command Structure

```bash
wacli groups join --code <INVITE_CODE>

```

The invite code is the final segment of the URL. For example, if the invite link is `https://chat.whatsapp.com/ABcDefGhIjK`, the code is `ABcDefGhIjK`.

### CLI Execution Flow

The CLI command performs the following operations:

1. Parses the `--code` flag.
2. Creates an `app` instance via `newApp()`, which initializes the high-level façade over the WhatsMeow client.
3. Ensures authentication via `EnsureAuthed()`.
4. Establishes connection via `Connect()`.
5. Executes `JoinGroupWithLink` at line 143 of [`cmd/wacli/groups_invite_join.go`](https://github.com/steipete/wacli/blob/main/cmd/wacli/groups_invite_join.go):

```go
// cmd/wacli/groups_invite_join.go line 143
jid, err := a.WA().JoinGroupWithLink(ctx, code)

```

6. Optionally fetches and persists group metadata if `GetGroupInfo` succeeds.

## Programmatic Implementation in Go

While the CLI provides convenience, you can invoke the same methods directly from Go code to **use invite links programmatically with wacli** within your own applications.

### Joining a Group Programmatically

```go
package main

import (
	"context"
	"log"

	"github.com/steipete/wacli/internal/wa"
)

func main() {
	// Initialize client with store path
	client, err := wa.New(wa.Options{StorePath: "./wa_store.sqlite3"})
	if err != nil {
		log.Fatal(err)
	}

	// Connect (assume already authenticated)
	ctx := context.Background()
	if err := client.Connect(ctx, wa.ConnectOptions{AllowQR: false}); err != nil {
		log.Fatal(err)
	}

	// Invite code from URL
	inviteCode := "ABcDefGhIjK"

	// Join group
	jid, err := client.JoinGroupWithLink(ctx, inviteCode)
	if err != nil {
		log.Fatalf("Failed to join: %v", err)
	}
	log.Printf("Successfully joined group: %s", jid.String())
}

```

### Generating Invite Links

If you administer a group, generate or reset invite links using `GetGroupInviteLink`:

```go
package main

import (
	"context"
	"log"

	"github.com/steipete/wacli/internal/wa"
	"go.mau.fi/whatsmeow/types"
)

func main() {
	client, _ := wa.New(wa.Options{StorePath: "./wa_store.sqlite3"})
	ctx := context.Background()
	client.Connect(ctx, wa.ConnectOptions{AllowQR: false})

	// Parse group JID
	groupJID, _ := types.ParseJID("1234567890-123456@g.us")

	// Generate new link (reset=true invalidates old links)
	link, err := client.GetGroupInviteLink(ctx, groupJID, true)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("New invite link: %s", link)
}

```

### Complete Workflow Example

This example extracts the invite code from a URL, joins the group, and handles the result:

```go
package main

import (
	"context"
	"log"
	"strings"

	"github.com/steipete/wacli/internal/wa"
	"go.mau.fi/whatsmeow/types"
)

func extractCode(link string) string {
	parts := strings.Split(strings.TrimSpace(link), "/")
	return parts[len(parts)-1]
}

func main() {
	waClient, _ := wa.New(wa.Options{StorePath: "./wa_store.sqlite3"})
	ctx := context.Background()
	waClient.Connect(ctx, wa.ConnectOptions{AllowQR: false})

	// Example: obtain a fresh link first
	groupJID, _ := types.ParseJID("1234567890-123456@g.us")
	freshLink, _ := waClient.GetGroupInviteLink(ctx, groupJID, true)
	code := extractCode(freshLink)

	joinedJID, err := waClient.JoinGroupWithLink(ctx, code)
	if err != nil {
		log.Fatalf("Join failed: %v", err)
	}
	log.Printf("Joined via freshly-generated link: %s", joinedJID.String())
}

```

## Key Source Files and Architecture

Understanding the codebase structure helps when extending or debugging **invite link functionality in wacli**:

| File | Role |
|------|------|
| [`internal/wa/groups.go`](https://github.com/steipete/wacli/blob/main/internal/wa/groups.go) | Contains `GetGroupInviteLink` and `JoinGroupWithLink` methods that wrap the WhatsMeow library. |
| [`cmd/wacli/groups_invite_join.go`](https://github.com/steipete/wacli/blob/main/cmd/wacli/groups_invite_join.go) | Implements the `wacli groups join` CLI command; parses the `--code` flag and orchestrates the join operation. |
| [`internal/app/app.go`](https://github.com/steipete/wacli/blob/main/internal/app/app.go) | Provides the `App.WA()` accessor used by CLI commands to reach the underlying WA client. |
| [`internal/wa/client.go`](https://github.com/steipete/wacli/blob/main/internal/wa/client.go) | Handles client lifecycle (authentication, connection) required before any group operation can succeed. |
| [`cmd/wacli/groups.go`](https://github.com/steipete/wacli/blob/main/cmd/wacli/groups.go) | Registers the `join` sub-command alongside other group management commands. |

The architecture follows a clear separation: the `internal/wa` package provides the low-level WhatsApp client abstraction, `internal/app` offers a higher-level application context, and `cmd/wacli` provides the user-facing CLI interface.

## Summary

- **CLI approach**: Run `wacli groups join --code <CODE>` where `<CODE>` is the final segment of the invite URL.
- **Go API approach**: Call `JoinGroupWithLink(ctx, code)` from [`internal/wa/groups.go`](https://github.com/steipete/wacli/blob/main/internal/wa/groups.go) after initializing and connecting the client.
- **Link generation**: Administrators can obtain or reset invite links via `GetGroupInviteLink(ctx, groupJID, reset)` in the same package.
- **Authentication required**: Both methods require an authenticated and connected client, handled automatically by the CLI or manually via `client.Connect()` in Go code.
- **Source locations**: Core logic lives in [`internal/wa/groups.go`](https://github.com/steipete/wacli/blob/main/internal/wa/groups.go), while the CLI wrapper is in [`cmd/wacli/groups_invite_join.go`](https://github.com/steipete/wacli/blob/main/cmd/wacli/groups_invite_join.go).

## Frequently Asked Questions

### How do I extract the invite code from a WhatsApp group link?

The invite code is the final segment of the URL after the last forward slash. For example, in `https://chat.whatsapp.com/ABcDefGhIjK`, the code is `ABcDefGhIjK`. You can extract this programmatically using standard string splitting operations in any language, or use the `strings.Split` function in Go as shown in the workflow examples above.

### Can I generate a new invite link for a group I administer using wacli?

Yes. Use the `GetGroupInviteLink` method from [`internal/wa/groups.go`](https://github.com/steipete/wacli/blob/main/internal/wa/groups.go) with the `reset` parameter set to `true`. This invalidates the previous link and returns a fresh one. The method requires you to pass the group's JID (e.g., `1234567890-123456@g.us`) and a boolean indicating whether to reset the existing link.

### What happens if the invite link is invalid or expired when using JoinGroupWithLink?

The `JoinGroupWithLink` method returns an error if the code is invalid, expired, or if the group has reached its member limit. Your code should always check the error return value and handle failure cases appropriately. The CLI implementation in [`cmd/wacli/groups_invite_join.go`](https://github.com/steipete/wacli/blob/main/cmd/wacli/groups_invite_join.go) demonstrates this by logging the error and exiting with a non-zero status code when the join operation fails.

### Is authentication required before calling JoinGroupWithLink or GetGroupInviteLink?

Yes. Both methods require an authenticated and connected WhatsApp client. The CLI handles this automatically through `EnsureAuthed()` and `Connect()` calls before executing the join operation. When using the Go API directly, you must call `client.Connect()` with valid credentials stored in the SQLite database at your specified `StorePath` before attempting any group operations.