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

# CLIs And Desktop Apps

> Connect Dari Link from a CLI or desktop app with any OAuth library.

A CLI can't keep a secret, so it signs in with standard OAuth 2.0: authorization code with PKCE and a loopback redirect ([RFC 8252](https://www.rfc-editor.org/rfc/rfc8252)). Use your language's OAuth library.

| Setting | Value |
| - | - |
| Authorization URL | `https://routing.dari.dev/v1/link/authorize` |
| Token URL | `https://routing.dari.dev/v1/link/token` |
| Client ID | Your app's `client_id` |
| Client secret | None |
| Redirect URI | `http://127.0.0.1/callback`, on any port |
| PKCE | `S256`, required |

## Register The App

In the [console](https://console.dari.dev), under **Apps**, create a **Browser Or Native** app with the redirect URI `http://127.0.0.1/callback`. Dari accepts it on any port, so your CLI can listen on whichever port is free.

## Sign In

With [`golang.org/x/oauth2`](https://pkg.go.dev/golang.org/x/oauth2):

```go Go theme={null}
package main

import (
	"context"
	"fmt"
	"net"
	"net/http"

	"golang.org/x/oauth2"
)

var conf = &oauth2.Config{
	ClientID: "lnk_…",
	Endpoint: oauth2.Endpoint{
		AuthURL:   "https://routing.dari.dev/v1/link/authorize",
		TokenURL:  "https://routing.dari.dev/v1/link/token",
		AuthStyle: oauth2.AuthStyleInParams,
	},
}

// signIn opens Dari Link in the browser and returns the user's tokens.
func signIn(ctx context.Context) (*oauth2.Token, error) {
	ln, err := net.Listen("tcp", "127.0.0.1:0") // any free port
	if err != nil {
		return nil, err
	}
	defer ln.Close()
	conf.RedirectURL = fmt.Sprintf("http://%s/callback", ln.Addr())

	state, verifier := oauth2.GenerateVerifier(), oauth2.GenerateVerifier()
	codes := make(chan string, 1)
	go http.Serve(ln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Query().Get("state") != state {
			http.Error(w, "Sign-in failed. Run the command again.", http.StatusBadRequest)
			codes <- ""
			return
		}
		fmt.Fprintln(w, "Connected. You can close this tab.")
		codes <- r.URL.Query().Get("code")
	}))

	fmt.Println("Open this link to connect your AI plan:")
	fmt.Println(conf.AuthCodeURL(state, oauth2.S256ChallengeOption(verifier)))
	code := <-codes
	if code == "" {
		return nil, fmt.Errorf("sign-in failed")
	}
	return conf.Exchange(ctx, code, oauth2.VerifierOption(verifier))
}
```

Save the token in the user's config directory, readable only by them (`0600`).

## Make Requests

```go Go theme={null}
ts := conf.TokenSource(ctx, token)
client := oauth2.NewClient(ctx, ts) // refreshes the access token as it expires

resp, err := client.Post("https://routing.dari.dev/v1/chat/completions", "application/json",
	strings.NewReader(`{"model": "dari/routing", "messages": [{"role": "user", "content": "Hi"}]}`))

// Each refresh returns a new refresh token and ends the old one. Save it.
latest, err := ts.Token()
```

If a refresh fails with `invalid_grant`, the user disconnected. Delete the saved token and sign in again.

## Other Languages

Any OAuth 2.0 library with PKCE works the same way with the settings above, such as [Authlib](https://docs.authlib.org/) for Python or [`oauth2`](https://docs.rs/oauth2) for Rust.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.