Skip to content

Commit c72b903

Browse files
committed
feat: add builder key auth actions
1 parent 5745c59 commit c72b903

3 files changed

Lines changed: 134 additions & 7 deletions

File tree

packages/bindings/src/clob/api-key.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { z } from 'zod';
2-
import { ApiKeySchema } from '../shared';
2+
import { ApiKeySchema, ISODateStringSchema } from '../shared';
33

44
export const RawApiKeyCredsSchema = z.object({
55
apiKey: ApiKeySchema,
@@ -20,3 +20,31 @@ export const ApiKeysResponseSchema = z.object({
2020
});
2121

2222
export type ApiKeysResponse = z.infer<typeof ApiKeysResponseSchema>;
23+
24+
export const BuilderApiKeyCredsSchema = z.object({
25+
key: ApiKeySchema,
26+
secret: z.string(),
27+
passphrase: z.string(),
28+
});
29+
30+
export type BuilderApiKeyCreds = z.infer<typeof BuilderApiKeyCredsSchema>;
31+
32+
export const BuilderApiKeySchema = z.object({
33+
key: ApiKeySchema,
34+
createdAt: ISODateStringSchema.optional(),
35+
revokedAt: ISODateStringSchema.nullable().optional(),
36+
});
37+
38+
export type BuilderApiKey = z.infer<typeof BuilderApiKeySchema>;
39+
40+
export const BuilderApiKeysResponseSchema = z
41+
.array(
42+
z.union([ApiKeySchema.transform((key) => ({ key })), BuilderApiKeySchema]),
43+
)
44+
.transform((apiKeys) =>
45+
apiKeys.map((apiKey) => BuilderApiKeySchema.parse(apiKey)),
46+
);
47+
48+
export type BuilderApiKeysResponse = z.infer<
49+
typeof BuilderApiKeysResponseSchema
50+
>;

packages/client/src/actions/auth.ts

Lines changed: 103 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import type { ApiKey } from '@polymarket/bindings';
12
import {
23
type ApiKeyCreds,
34
ApiKeyCredsSchema,
4-
type ApiKeysResponse,
55
ApiKeysResponseSchema,
6+
type BuilderApiKey,
7+
type BuilderApiKeyCreds,
8+
BuilderApiKeyCredsSchema,
9+
BuilderApiKeysResponseSchema,
610
} from '@polymarket/bindings/clob';
711
import { type EvmAddress, type Signature, unwrap } from '@polymarket/types';
812
import { z } from 'zod';
@@ -145,9 +149,7 @@ export type FetchApiKeysError =
145149
* @throws {@link FetchApiKeysError}
146150
* Thrown when request signing fails, or the request is rejected, rate limited, interrupted by transport issues, or returns an unexpected response.
147151
*/
148-
export async function fetchApiKeys(
149-
client: SecureClient,
150-
): Promise<ApiKeysResponse['apiKeys']> {
152+
export async function fetchApiKeys(client: SecureClient): Promise<ApiKey[]> {
151153
const response = await unwrap(
152154
client.secureClob
153155
.get('/auth/api-keys')
@@ -171,7 +173,9 @@ export type DeleteApiKeyError =
171173
* This is a low-level auth action that most SDK consumers will not need.
172174
*
173175
* @example
174-
*
176+
* ```ts
177+
* await deleteApiKey(client);
178+
* ```
175179
*
176180
* @throws {@link DeleteApiKeyError}
177181
* Thrown when request signing fails, or the request is rejected, rate limited,
@@ -185,6 +189,100 @@ export async function deleteApiKey(client: SecureClient): Promise<void> {
185189
);
186190
}
187191

192+
export type CreateBuilderApiKeyError =
193+
| RateLimitError
194+
| RequestRejectedError
195+
| SigningError
196+
| TransportError
197+
| UnexpectedResponseError;
198+
199+
/**
200+
* Creates a new builder API key for the authenticated client.
201+
*
202+
* @remarks
203+
* This is a low-level auth action that most SDK consumers will not need.
204+
*
205+
* @example
206+
* ```ts
207+
* const builderApiKey = await createBuilderApiKey(client);
208+
* ```
209+
*
210+
* @throws {@link CreateBuilderApiKeyError}
211+
* Thrown when request signing fails, or the request is rejected, rate limited,
212+
* interrupted by transport issues, or returns an unexpected response.
213+
*/
214+
export async function createBuilderApiKey(
215+
client: SecureClient,
216+
): Promise<BuilderApiKeyCreds> {
217+
return unwrap(
218+
client.secureClob
219+
.post('/auth/builder-api-key')
220+
.andThen(validateWith(BuilderApiKeyCredsSchema)),
221+
);
222+
}
223+
224+
export type FetchBuilderApiKeysError =
225+
| RateLimitError
226+
| RequestRejectedError
227+
| SigningError
228+
| TransportError
229+
| UnexpectedResponseError;
230+
231+
/**
232+
* Fetches builder API keys associated with the authenticated client.
233+
*
234+
* @remarks
235+
* This is a low-level auth action that most SDK consumers will not need.
236+
*
237+
* @example
238+
* ```ts
239+
* const builderApiKeys = await fetchBuilderApiKeys(client);
240+
* ```
241+
*
242+
* @throws {@link FetchBuilderApiKeysError}
243+
* Thrown when request signing fails, or the request is rejected, rate limited,
244+
* interrupted by transport issues, or returns an unexpected response.
245+
*/
246+
export async function fetchBuilderApiKeys(
247+
client: SecureClient,
248+
): Promise<BuilderApiKey[]> {
249+
return unwrap(
250+
client.secureClob
251+
.get('/auth/builder-api-key')
252+
.andThen(validateWith(BuilderApiKeysResponseSchema)),
253+
);
254+
}
255+
256+
export type RevokeBuilderApiKeyError =
257+
| RateLimitError
258+
| RequestRejectedError
259+
| SigningError
260+
| TransportError
261+
| UnexpectedResponseError;
262+
263+
/**
264+
* Revokes a builder API key.
265+
*
266+
* @remarks
267+
* This is a low-level auth action that most SDK consumers will not need.
268+
*
269+
* @example
270+
* ```ts
271+
* await revokeBuilderApiKey(client);
272+
* ```
273+
*
274+
* @throws {@link RevokeBuilderApiKeyError}
275+
* Thrown when request signing fails, or the request is rejected, rate limited,
276+
* interrupted by transport issues, or returns an unexpected response.
277+
*/
278+
export async function revokeBuilderApiKey(client: Client): Promise<void> {
279+
await unwrap(
280+
client.clob
281+
.del('/auth/builder-api-key')
282+
.andThen(validateWith(z.literal('OK'))),
283+
);
284+
}
285+
188286
function toL1Headers(auth: ApiKeyAuthRequest): HeadersInit {
189287
return {
190288
POLY_ADDRESS: auth.address,

packages/client/src/actions/orders.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { OrderSide, OrderType } from '@polymarket/bindings';
21
import {
32
type AcceptedOrderResponse,
43
OrderPostStatus,
54
type OrderResponse,
5+
OrderSide,
6+
OrderType,
67
} from '@polymarket/bindings/clob';
78
import { expectPresent } from '@polymarket/types';
89
import { describe, expect, it } from 'vitest';

0 commit comments

Comments
 (0)