repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
Mirrored from https://github.com/ProjectOpenSea/opensea-skill
stars
latest
clone command
git clone gitlawb://did:key:z6MkqRzA...RfoM/ProjectOpenSea-...git clone gitlawb://did:key:z6MkqRzA.../ProjectOpenSea-...fef93001Release v2.14.012h ago| #1 | #!/usr/bin/env bash |
| #2 | set -euo pipefail |
| #3 | |
| #4 | # Usage: opensea-resolve-key.sh [--force] |
| #5 | # |
| #6 | # Resolves an OpenSea API key using a deterministic, persistent flow so an |
| #7 | # instant key fetched once is reused on every later request instead of being |
| #8 | # re-fetched (or lost): |
| #9 | # |
| #10 | # 1. If OPENSEA_API_KEY is set in the environment, print it and stop. |
| #11 | # User-supplied keys always win — they are never overwritten or re-fetched. |
| #12 | # 2. Else if a key is cached on disk, print it. |
| #13 | # 3. Else fetch a fresh instant free-tier key, SAVE it to disk (so the next |
| #14 | # invocation reuses it), then print it. |
| #15 | # |
| #16 | # Prints ONLY the resolved key to stdout. Typical use: |
| #17 | # export OPENSEA_API_KEY=$(packages/skill/opensea-api/scripts/auth/opensea-resolve-key.sh) |
| #18 | # |
| #19 | # --force Ignore any cached key on disk and fetch a fresh instant key, then |
| #20 | # overwrite the cache. Use this when a cached instant key is rejected |
| #21 | # (HTTP 401/403 -> invalid or expired). It does NOT override a key set |
| #22 | # via the OPENSEA_API_KEY environment variable. |
| #23 | # |
| #24 | # Storage location: ${OPENSEA_CONFIG_DIR:-$HOME/.opensea}/api_key (mode 600). |
| #25 | |
| #26 | force=0 |
| #27 | if [ "${1:-}" = "--force" ]; then |
| #28 | force=1 |
| #29 | fi |
| #30 | |
| #31 | config_dir="${OPENSEA_CONFIG_DIR:-$HOME/.opensea}" |
| #32 | key_file="$config_dir/api_key" |
| #33 | |
| #34 | # 1. Environment always wins (preserves the user-supplied-key flow). |
| #35 | if [ -n "${OPENSEA_API_KEY:-}" ]; then |
| #36 | printf '%s\n' "$OPENSEA_API_KEY" |
| #37 | exit 0 |
| #38 | fi |
| #39 | |
| #40 | # 2. Reuse a cached key unless --force was passed. |
| #41 | if [ "$force" -eq 0 ] && [ -s "$key_file" ]; then |
| #42 | cat "$key_file" |
| #43 | exit 0 |
| #44 | fi |
| #45 | |
| #46 | # 3. Fetch a fresh instant key. |
| #47 | base="${OPENSEA_BASE_URL:-https://api.opensea.io}" |
| #48 | url="$base/api/v2/auth/keys" |
| #49 | |
| #50 | tmp_body=$(mktemp) |
| #51 | trap 'rm -f "$tmp_body"' EXIT |
| #52 | |
| #53 | http_code=$(curl -sS --connect-timeout 10 --max-time 30 -X POST \ |
| #54 | -H "User-Agent: opensea-skill/1.0" \ |
| #55 | -H "Content-Type: application/json" \ |
| #56 | -d '{}' \ |
| #57 | -w '%{http_code}' \ |
| #58 | -o "$tmp_body" \ |
| #59 | "$url") || { |
| #60 | echo "opensea-resolve-key.sh: curl transport error (exit $?)" >&2 |
| #61 | exit 1 |
| #62 | } |
| #63 | |
| #64 | if [[ ! "$http_code" =~ ^2 ]]; then |
| #65 | if [ "$http_code" = "429" ]; then |
| #66 | echo "opensea-resolve-key.sh: HTTP 429 rate limited (instant key creation is rate limited per IP). Reuse the cached key if you have one, try again later, or create a key at https://opensea.io/settings/developer" >&2 |
| #67 | else |
| #68 | echo "opensea-resolve-key.sh: HTTP $http_code error while requesting an instant key" >&2 |
| #69 | fi |
| #70 | cat "$tmp_body" >&2 |
| #71 | exit 1 |
| #72 | fi |
| #73 | |
| #74 | api_key=$(jq -r '.api_key // empty' "$tmp_body") |
| #75 | if [ -z "$api_key" ]; then |
| #76 | echo "opensea-resolve-key.sh: response did not contain an api_key" >&2 |
| #77 | cat "$tmp_body" >&2 |
| #78 | exit 1 |
| #79 | fi |
| #80 | |
| #81 | # Persist for reuse. Best-effort: still print the key even if saving fails so |
| #82 | # the current request can proceed. |
| #83 | if mkdir -p "$config_dir" 2>/dev/null; then |
| #84 | if (umask 077; printf '%s\n' "$api_key" > "$key_file") 2>/dev/null; then |
| #85 | chmod 600 "$key_file" 2>/dev/null || true |
| #86 | else |
| #87 | echo "opensea-resolve-key.sh: warning: could not write $key_file (key not cached)" >&2 |
| #88 | fi |
| #89 | else |
| #90 | echo "opensea-resolve-key.sh: warning: could not create $config_dir (key not cached)" >&2 |
| #91 | fi |
| #92 | |
| #93 | printf '%s\n' "$api_key" |
| #94 |