#!/usr/bin/env bash # LaRouter setup for Claude CLI. # Adds the LaRouter address and your API key to ~/.claude/settings.json, # keeps every other setting, saves a backup first and creates the file if it is missing. # Usage: curl -fsSL https://larouter.lapage.vn/setup/claude.sh | LAROUTER_KEY='your-key' bash larouter_setup() { if [ -z "$LAROUTER_KEY" ] || [ "$LAROUTER_KEY" = "YOUR_API_KEY" ]; then echo "Your LaRouter API key is missing. Sign in on the setup page and copy the command again, or replace YOUR_API_KEY with your key. Nothing was changed." >&2 return 1 fi perl -MJSON::PP -e ' my $dir = "$ENV{HOME}/.claude"; my $p = "$dir/settings.json"; my $d = {}; mkdir $dir unless -d $dir; if (-s $p) { open(my $in, "<", $p) or die "Cannot read $p\n"; local $/; my $text = <$in>; close $in; $d = eval { JSON::PP->new->utf8->decode($text) }; die "Your $p is not valid JSON. Nothing was changed.\n" unless ref $d eq "HASH"; my @t = localtime; my $bak = sprintf("%s.bak-%04d%02d%02d-%02d%02d%02d", $p, $t[5]+1900, $t[4]+1, @t[3,2,1,0]); open(my $b, ">", $bak) or die "Cannot write backup\n"; print $b $text; close $b; print "Backup saved: $bak\n"; } $d->{env} = {} unless ref $d->{env} eq "HASH"; $d->{env}{ANTHROPIC_BASE_URL} = "https://i-larouter.lapage.vn/v1"; $d->{env}{ANTHROPIC_AUTH_TOKEN} = $ENV{LAROUTER_KEY}; open(my $out, ">", "$p.tmp") or die "Cannot write $p\n"; print $out JSON::PP->new->utf8->pretty->canonical->encode($d); close $out; rename("$p.tmp", $p) or die "Cannot save $p\n"; print "Done. LaRouter is set up in $p\n"; ' } larouter_setup