test_api.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/bin/bash
  2. BASE_URL="${LETTA_SCHEDULES_URL:-https://your-modal-app-url.modal.run}"
  3. AGENT_ID="${LETTA_AGENT_ID:-your-agent-id}"
  4. API_KEY="${LETTA_API_KEY}"
  5. if [ -z "$API_KEY" ]; then
  6. echo "ERROR: LETTA_API_KEY environment variable not set!"
  7. echo "This must be a VALID Letta API key that will be validated against Letta's API."
  8. echo "Set it with: export LETTA_API_KEY=sk-..."
  9. exit 1
  10. fi
  11. echo "========================================"
  12. echo "Letta Schedules API Test (curl)"
  13. echo "========================================"
  14. echo "Base URL: $BASE_URL"
  15. echo "Agent ID: $AGENT_ID"
  16. echo "API Key: ${API_KEY:0:20}..."
  17. echo ""
  18. echo "1. Creating recurring schedule (every 5 minutes)..."
  19. RECURRING_RESPONSE=$(curl -s -X POST "$BASE_URL/schedules/recurring" \
  20. -H "Content-Type: application/json" \
  21. -d "{
  22. \"agent_id\": \"$AGENT_ID\",
  23. \"api_key\": \"$API_KEY\",
  24. \"cron\": \"*/5 * * * *\",
  25. \"message\": \"Test recurring message\",
  26. \"role\": \"user\"
  27. }")
  28. echo "$RECURRING_RESPONSE" | python3 -m json.tool
  29. RECURRING_ID=$(echo "$RECURRING_RESPONSE" | python3 -c "import sys, json; print(json.load(sys.stdin)['id'])" 2>/dev/null)
  30. echo "Recurring Schedule ID: $RECURRING_ID"
  31. echo ""
  32. echo "2. Creating one-time schedule (2 minutes from now)..."
  33. EXECUTE_AT=$(python3 -c "from datetime import datetime, timedelta, timezone; print((datetime.now(timezone.utc) + timedelta(minutes=2)).isoformat())")
  34. ONETIME_RESPONSE=$(curl -s -X POST "$BASE_URL/schedules/one-time" \
  35. -H "Content-Type: application/json" \
  36. -d "{
  37. \"agent_id\": \"$AGENT_ID\",
  38. \"api_key\": \"$API_KEY\",
  39. \"execute_at\": \"$EXECUTE_AT\",
  40. \"message\": \"Test one-time message\",
  41. \"role\": \"user\"
  42. }")
  43. echo "$ONETIME_RESPONSE" | python3 -m json.tool
  44. ONETIME_ID=$(echo "$ONETIME_RESPONSE" | python3 -c "import sys, json; print(json.load(sys.stdin)['id'])" 2>/dev/null)
  45. echo "One-Time Schedule ID: $ONETIME_ID"
  46. echo ""
  47. echo "3. Listing all recurring schedules..."
  48. curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/schedules/recurring" | python3 -m json.tool
  49. echo ""
  50. echo "4. Listing all one-time schedules..."
  51. curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/schedules/one-time" | python3 -m json.tool
  52. echo ""
  53. if [ ! -z "$RECURRING_ID" ]; then
  54. echo "5. Getting specific recurring schedule..."
  55. curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/schedules/recurring/$RECURRING_ID" | python3 -m json.tool
  56. echo ""
  57. fi
  58. if [ ! -z "$ONETIME_ID" ]; then
  59. echo "6. Getting specific one-time schedule..."
  60. curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/schedules/one-time/$ONETIME_ID" | python3 -m json.tool
  61. echo ""
  62. fi
  63. echo "7. Listing execution results..."
  64. curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/results" | python3 -m json.tool
  65. echo ""
  66. if [ ! -z "$RECURRING_ID" ]; then
  67. echo "8. Getting execution result for recurring schedule..."
  68. curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/results/$RECURRING_ID" | python3 -m json.tool
  69. echo ""
  70. fi
  71. if [ ! -z "$ONETIME_ID" ]; then
  72. echo "9. Getting execution result for one-time schedule..."
  73. curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/results/$ONETIME_ID" | python3 -m json.tool
  74. echo ""
  75. fi
  76. read -p "Press Enter to delete test schedules..."
  77. if [ ! -z "$RECURRING_ID" ]; then
  78. echo "Deleting recurring schedule..."
  79. curl -s -X DELETE -H "Authorization: Bearer $API_KEY" "$BASE_URL/schedules/recurring/$RECURRING_ID" | python3 -m json.tool
  80. echo ""
  81. fi
  82. if [ ! -z "$ONETIME_ID" ]; then
  83. echo "Deleting one-time schedule..."
  84. curl -s -X DELETE -H "Authorization: Bearer $API_KEY" "$BASE_URL/schedules/one-time/$ONETIME_ID" | python3 -m json.tool
  85. echo ""
  86. fi
  87. echo "Final results after deletion..."
  88. curl -s -H "Authorization: Bearer $API_KEY" "$BASE_URL/results" | python3 -m json.tool
  89. echo ""
  90. echo "========================================"
  91. echo "Test complete!"
  92. echo "========================================"
  93. echo ""
  94. echo "Note: Execution results remain even after schedules are deleted."
  95. echo "Check run status at: https://api.letta.com/v1/runs/{run_id}"