File size: 3,874 Bytes
0240df9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Cursor To OpenAI

Convert the Cursor Editor to an OpenAI API interface service.

## Introduction

This project provides a proxy service that converts the AI chat of the Cursor Editor into an OpenAPI API, allowing you to reuse the LLM of the Cursor in other applications.

## Preparsuitue

Visit [Cursor](https://www.cursor.com) and register a account.
- 150 fast premium requests are given, which can be reset by deleting the account and then registering again
- Suggest to use gmail/outlook email, some temp emails have been disabled by Cursor.

### Get Cursor client cookie

The cookie from Cursor webpage does not work in Cursor-To-OpenAI server. You need to get the Cursor client cookie following these steps:

1. Run `npm install` to initialize the environment。
2. Run `npm run login`. Open the URL shows in the log, and then login your account.
3. The cookie shows in your command is the `Curosr Cookie` value. Copy and save it to your notepad.

The log of this command looks like:
```
[Log] Please open the following URL in your browser to login:
https://www.cursor.com/loginDeepControl?challenge=6aDBevuHkK-HLiZ<......>k2lEjbVRMpg&uuid=5147ac09<....>5fe5f3aeb&mode=login      <-- Copy the url and open it in your browser.
[Log] Waiting for login... (1/60)
[Log] Waiting for login... (2/60)
[Log] Waiting for login... (3/60)
[Log] Waiting for login... (4/60)
[Log] Login successfully. Your Cursor cookie:
user_01JJF<.....>K3F4T8%3A%3AeyJhbGciOiJIUzI1NiIsInR5cCI6Ikp<...................>AsCpbPfnlHy022WxmlKIt4Q7Ll0     <-- This is the Cursor cookie, please save it.
```

#### API to get Cursor client cookie

We provide an API to save you from manual login. You need to log in to your Cursor account in browser and get `WorkosCursorSessionToken` from Application-Cookie.
1. Get Cursor client cookie
    - Url:`http://localhost:3010/cursor/loginDeepContorl`
    - Request:`GET`
    - Authentication:`Bearer Token`(The value of `WorkosCursorSessionToken` from Cursor webpage)
    - Reponse: In JSON, the value of `accessToken` is the `Cursor Cookie` in JWT format. That's what you want.

Sample request:
```
import requests

WorkosCursorSessionToken = "{{{Repalce by your WorkosCursorSessionToken from cookie in browser}}}}"
response = requests.get("http://localhost:3010/cursor/loginDeepControl", headers={
    "authorization": f"Bearer {WorkosCursorSessionToken}"
})
data = response.json()
cookie = data["accessToken"]
print(cookie)
```

## How to Run

### Run in docker
```
docker run -d --name cursor-to-openai -p 3010:3010 ghcr.io/jiuz-chn/cursor-to-openai:latest
```

### Run in npm
```
npm install
npm run start
```

## How to use the server

1. Get models
    - Url:`http://localhost:3010/v1/models`
    - Request:`GET`
    - Authentication:`Bearer Token`(The value of `Cursor Cookie`)

2. Chat completion
    - Url:`http://localhost:3010/v1/chat/completions`
    - Request:`POST`
    - Authentication:`Bearer Token`(The value of `Cursor Cookie`,supports comma-separated values)

 for the response body, please refer to the OpenAI interface

### Python demo
```
from openai import OpenAI

client = OpenAI(api_key="{{{Replace by the Cursor cookie of your account. It starts with user_...}}}",
                base_url="http://localhost:3010/v1")

response = client.chat.completions.create(
    model="claude-3-7-sonnet",
    messages=[
        {"role": "user", "content": "Hello."},
    ],
    stream=False
)

print(response.choices)
```

## Notes

- Please keep your Cursor cookie properly and do not disclose it to others
- This project is for study and research only, please abide by the Cursor Terms of Use

## Acknowledgements

- This project is based on [cursor-api](https://github.com/zhx47/cursor-api)(by zhx47).
- This project integrates the commits in [cursor-api](https://github.com/lvguanjun/cursor-api)(by lvguanjun).