npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@0x/swap-ts-sdk

v2.1.1

Published

0x Swap API TypeScript Client

Downloads

213

Readme

@0x/swap-ts-sdk

A TypeScript client to interact with 0x API. Currently @0x/swap-ts-sdk supports 0x API v2 "Swap" and "Gasless" endpoints.

Note: @0x/swap-ts-sdk is meant for server side use only. Using the SDK from a browser is not supported.

Setup

pnpm add -E @0x/swap-ts-sdk

Important: TypeScript needs to be configured with compilerOptions.strict set to true. The client won't correctly type check if TypeScript is not in strict mode.

Visit 0x.org to get your API key.

Usage

Create a "vanilla" Node client with createClientV2:

import { createClientV2 } from '@0x/swap-ts-sdk';

const client = createClientV2({
    apiKey: '33da2...91ebf9',
});

const price = await client.swap.permit2.getPrice.query({
    buyToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
    chainId: 1,
    sellAmount: '1000000000000000000',
    sellToken: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
});

API reference

Visit docs.0x.org for the API specification.

Client documentation

The @0x/swap-ts-sdk client is a wrapped & typed tRPC v10.x client.

Visit https://trpc.io/docs/v10/client for full documentation, including how to use the client with Next.js, React Query, or vanilla Node.

Aborting calls (timeout)

import { createClientV2 } from '@0x/swap-ts-sdk';

const client = createClientV2({
    apiKey: '33da2...91ebf9',
});

const quote = await client.gasless.getQuote.query(
    {
        buyToken: '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
        chainId: 1,
        sellAmount: '1000000000000000000',
        sellToken: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
        taker: '0x60B4f0e1DF30c8c0f0b0c8BEc8787E7564647a80',
        txOrigin: '0x60B4f0e1DF30c8c0f0b0c8BEc8787E7564647a80',
    },
    {
        signal: AbortSignal.timeout(1000),
    },
);

Using with Next.js

You can use @trpc/next directly to use the SDK with Next.js. See the documentation here: https://trpc.io/docs/v10/client/nextjs/ssr.

To type the client, the packages exports the router type:

import type { RouterV2 } from '@0x/swap-ts-sdk';
import { httpLink } from '@trpc/client';
import { createTRPCNext } from '@trpc/next';

export const trpc = createTRPCNext<RouterV2>({
    config(_opts) {
        return {
            links: [
                httpLink({
                    headers: {
                        '0x-api-key': 'your-api-key',
                        '0x-version': 'v2',
                    },
                    url: 'https://api.0x.org/trpc/swap',
                }),
            ],
        };
    },
    ssr: true,
});