Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import * as HttpClient from "effect/http/HttpClient";
4import * as HttpClientRequest from "effect/http/HttpClientRequest";
5import * as HttpClientResponse from "effect/http/HttpClientResponse";
6
7export const DEFAULT_CURSOR_API_ENDPOINT = "https://api2.cursor.sh";
8
9const ApiKeyExchange = Schema.Struct({ accessToken: Schema.NonEmptyString });
10
11/** Trades a user API key for the access token Cursor's RPCs accept, as `@cursor/sdk` does. */
12export const exchangeCursorApiKey = Effect.fn("exchangeCursorApiKey")(function* (apiKey: string) {
13 const client = yield* HttpClient.HttpClient;
14 const response = yield* client.execute(
15 HttpClientRequest.post(`${DEFAULT_CURSOR_API_ENDPOINT}/auth/exchange_user_api_key`).pipe(
16 HttpClientRequest.bearerToken(apiKey),
17 HttpClientRequest.bodyJsonUnsafe({}),
18 ),
19 );
20 const { accessToken } = yield* HttpClientResponse.schemaBodyJson(ApiKeyExchange)(
21 yield* HttpClientResponse.filterStatusOk(response),
22 );
23 return accessToken;
24}, Effect.timeout("10 seconds"));